Hi and happy new year to everyone!
It has been a while since my last post so here is a new one, that some of you may find useful.
This one is about handling internationalization (i18n) in an iOS App: this is not always as easier as it seems, especially when it comes to maintaining translations in between each application release.
When working with iOS SDK there is already a set of functions / tools to help you managing your i18n.
First, you have to use the NSLocalizedString function to make your strings internationalized.This is pretty easy: the first parameter is the key, the second the comment. However don’t use a code (like in a Java property file) as key but the default string value: it will make things easier when generating localized string files.
Here is an example:
NSString *myLocalizedString = NSLocalizedString(@”Edit”, @”Edit button label”);
Providing a comment is not mandatory but can be very useful to locate the string in the application when translating.
If your string as variable parts, your can also use variables in the string, like this:
NSString *myLocalizedString = [NSString stringWithFormat:NSLocalizedString(@”Hello %@”, @”Welcome message”), @”Bob”];
Once strings are prepared for localization, they must be written down to a file for translation work.
For this task, Apple provides a command line tool: genstrings, that browses source files to extract strings to localize.
It can be used like this:
genstrings -o en.lproj *.m
This command will extract all i18n strings from .m files in the current directory and will write them in the en.lproj directory. Of course, en.lproj can be changed to any locale you want to extract to. If your keys are written in french in your code, use fr.lproj instead.
As in most of my projects, sources are spread in different folders / sub-folders, I wrote a shell script to automate the task, here it is:
# Script to regenerate en Strings
find . -name \*.m | xargs genstrings -o Resources/Strings/en.lproj
When you need to add a language, run the command with another locale and translate the strings in the generated file.
The above solution works fine for the initial translation of the application, but with updates and new releases, it becomes hard to maintain.
As a matter of fact, if you need to modify a string or add a new one, you have to generate the strings file for each language again and all your previous translations will be lost (you can also add every new string by hand in every file, but you will get lost soon…).
Hopefully, I had the chance to find a set of free tools named Localization Suite. This suite provides tools to manage and translate strings of an application.
It contains 3 applications:
To use the suite, you first have to import your projet strings into the Localization manager: this is pretty easy as you can import an XCode projet from it.
Then, from here you can mange languages and check if localization is complete for each of them.
To translate into a given language, select it and click ‘Edit…’ to open it the Localizer application.
You need to click ‘Write Out’ to write the Localizer file for the given language the first time you use it.
The Localizer is a clear editor where you can see the keys you have to translate and their translation in the default language. You can also filter keys to show only missing ones.
Once the translation is complete, save the file, then go back to the Localization manager, select the language you just translated and click ‘Read In…’ to import your localized file into the database.
Repeat the operation for each language you want to translate (‘Edit…’ language file, translate, save and import with ‘Read In…’).
When the translation work is finished, click the ‘Synchronize’ button (the above one, near ‘Re-Inject’): the new strings files are exported to your XCode project.
Make sure to keep your Location manager database (.ldb) and Localizer files (.loc) in a safe place for latter use.
The main advantage of the Localization suite is that you can easly update your application strings.
To do so:
There are probably other ways and tools to perform the same operations, but I have been using this one for some times now and I have never been disappointed!
The fact that the Localization suite comes with 3 different applications can confuse the user at the beginning (especially when you have to export / import from / to the Localization manager and the Localizer), but once you got it, it is pretty straight forward!
Note that the Localization suite works with localized XIB files too.