In a previous post I discussed options for internationalizing iOS apps. In this addendum to that article I’m sharing an amazing discovery that has saved me so much work and time. You can actually take all of the strings out of a XIB file using the
ibtool command in the terminal. You then translate the resulting
.strings file into the language of your choice and generate a new localized XIB file from the original one using the same command line tool.
Let’s follow a simple example. In my app I have a XIB file (
WaitViewController.xib) I need to localize into three languages. The original version is in English.
- If not already so, we need to make the
WaitViewController.xibfile localized. We can do this by either moving it manually into the
en.lprojfolder and then adjusting our project’s references, or in the project itself by using the File Inspector to add a new localization, which will perform the same move and adjust references all at once.
- In the Terminal application, run the
ibtoolcommand in the project’s folder that contains the
en.lprojdirectory with the following switches
ibtool extractShell1ibtool --generate-strings-file en.lproj/WaitViewController.strings en.lproj/WaitViewController.xib
The –generate-strings-file option specifies what file will be created, while the parameter indicates what XIB file will be examined to extract strings from.
- Now we have the English localisation file with all the strings used in the XIB file. We have to translate it into the languages required by our app. For example, I need Spanish and Italian, so I will have
it.lproj/WaitViewController.stringsonce the translation work has been done.
- The localized XIB files must already be present in your project structure for the next step to work, so we simply copy the English original into the appropriate language folders. You can also use the File Inspector again to create the localized versions.
- Last thing, given the translated
.stringsfile, we modify the localized XIBs based on the original (English) one, but with text from the localized strings. It’s the
ibtoolcommand again that does the job.
ibtool extractShell12ibtool --strings-file es.lproj/WaitViewController.strings --write es.lproj/WaitViewController.xib en.lproj/WaitViewController.xibibtool --strings-file it.lproj/WaitViewController.strings --write it.lproj/WaitViewController.xib en.lproj/WaitViewController.xib
At this point your XIB files are localized and ready to use with no changes to your code. The
.strings file become unnecessary once you have localized XIBs and can be deleted.