Since your app can be downloaded in almost every country of the world, the support of multiple languages is a crucial topic in app development. In this post I will show a simple example of how you can localize strings used within your app.
To consequently use the predefined foundation macros within your code files is the best way to start. The macros (a detailed description can be found over here) are:
- NSLocalizedString(key, comment)
- NSLocalizedStringFromTable(key, tableName, comment)
- NSLocalizedStringFromTableInBundle(key, tableName, bundle, comment)
- NSLocalizedStringWithDefaultValue(key, tableName, bundle, value, comment)
The really best choice is to use NSLocalizedStringWithDefaultValue, because with this macro you are able to define your key and a corresponding default value. The default value should be the correct translation for the given key in your main development language. A simple example is:
NSLocalizedStringWithDefaultValue(@"firstname",
nil,
[NSBundle mainBundle],
@"Firstname",
@"user information");
As you can see, I want to get a translation for my key firstname. Since translations are stored in .strings files, the parameter tableName specifies the name of the file you want to search for the given key. If this parameter is empty the default Localizable.strings will be searched. Also you have to specify the bundle, where your localized resources reside. Mostly it will be the main bundle of your application. The next two parameters are default and comment which will be very useful as you will see soon.
So when using this macro, your application should be fully functional within your main development language. Before starting to translate your application you need to create some language specific folders within your application folder. These folders need to have the .lproj extension and their name should be the language abbreviation you want to support (i.e. en, de, fr, …).
Now there are two options to go. Either you create a Localizable.strings file in each .lproj directory and add your key-value-pairs manually or you generate the first .string file from you source code. Since we consequently used the predefined macros it is possible to use the command line tool genstrings to parse all your code files and create the corresponding .strings file. To do so, simply type in:
genstrings -o en.lproj Classes/*.m
After that you get your Localizable.strings file with following content:
/* user information */
"firstname" = "Firstname";
As you can see this is very simple. Also the given comment used within the macro call is visible within the generated code which allows you to keep records about your keys in a nice manner. Now you can simply copy your file to all other directories and translate them. The only trade-off you should keep in mind is that genstrings overwrites your file every time you call it. This means you should keep a copy of your files before calling genstrings again.
Multilanguage support within your .nib files or images is quite easy too, just create translated versions of your files and place them in the corresponding .lproj directory.
Tip:
If you are struggling with the localization and internationalization support within XCode, because it creates folders named like English.lproj, German.lproj, … then simply create the folders en.lproj, de.lproj, … within your application directory by your own. After that, place a file like Localizable.strings in your sub folders and then add the file within XCode to your project.
So that’s all folks and I hope you enjoyed this post.
Cheers,
Andreas