Working With Localized Strings

Localized strings are the one resource type other than executable code that CFBundle is able to load when located. The string API consists of only one primary function-- CFBundleCopyLocalizedString --along with a few macros defined for convienience. In practice, you should generally use the CFLocalizedString macros in your code. These macros all call CFBundleCopyLocalizedString , but they provide a couple of important additional features. First, there are several variants of the macro that are simpler to use for certain common cases. Also, these macros are recognized by the genstrings development tool, which can automatically generate strings files from your source code, but only if it uses these macros. Lastly, the macros include a comment string argument intended to help the translator. This parameter is ignored by the compiler, but genstrings uses it to annotate the generated strings file. Listing 1-11 demonstrates the proper usage of the three localized string macros.

Listing 1-11 Using the LocalizedString macros
CFStringRef localString; localString = CFLocalizedString( CFSTR("Development region version of string"), CFSTR("Comment to help translators by giving them \ context or other hints about how the string is used \ or how to translate it.")); localString = CFLocalizedStringFromTable( CFSTR("Development region version of string"), CFSTR("MyStrings"), // strings file to search CFSTR("Comment to help translators.")); localString = CFLocalizedStringFromTableInBundle( CFSTR("Development region version of string"), CFSTR("MyStrings"), // strings file to search myBundle, // bundle to search CFSTR("Comment to help translators."));

The first version of the macro attempts to find your string using the key "Development region version of string" in a strings file named Localizable.strings in the application's main bundle. The second version looks in the strings file named MyStrings.strings in the main bundle. The last one lets you specify all the details. The macros use the first argument as both the key and the translation for the development region.


© 2000 Apple Computer, Inc. (Last Updated 30 June 2000)