Creating string files for your iOS app
This tutorial assumes that your application is only available in 1 language. The examples shown in this guide are designed as if the app were in English.
Introduction:
For this tutorial we have created an example so that you can see how simple it is to have your app in different languages.
The strings for the two upper labels (top labels) are defined in the .m file, while the two lower texts are defined in the .xib file.
We recommend loading all the string files in .m files because it makes the whole process much easier. However, this tutorial will explain how to do it by locating .xib files as well, if you prefer to do it this way.
Creating localizable.strings files:
First of all, we have to check all the strings in our code to indicate that from now on it will be directed to the file Localizable.strings (which we haven’t created yet), where it will find the text to use.
Therefore, you will have to change every petition like:
[NSString stringWithFormat:@"texto del string"]
To
[NSString stringWithFormat:NSLocalizedString(@"texto del string ",comment)]
As you can see, NSLocalizedString contains 2 actions. The second parameter can be a comment of the developer or even for the translator. It isn’t used for anything regarding the development. To keep it simple we have used “nil” in this example.
If NSLocalizedString searches in the string file for a string, which cannot be found there, simply the first one with the introduced parameters will be shown.
Remember that you don’t have to translate or locate strings the user never will see. Therefore, concentrate on translating and locating all the texts of your app, which could be seen by the users.
The next step will be to open a terminal. Once you’ve opened it, write “cd” and then drag the folder over the console and press ENTER.
Then, use the tool “genstrings” to automatically generate a string file for our .m files.
Therefore, write the following and press ENTER:
genstrings -o en.lproj *.m
This will create a Localizable.strings file in your folder en.lproj. This file will look like this:
Every line is a key with its value. For this string file in English both have the same value, because you want English-speaking users to see your strings by default. Nevertheless, if your string file is in Spanish for example, you will see it this way:
“Hello world, this is an example” = “Hola mundo, esto es un ejemplo”
“This is an example” = “Esto es un ejemplo”
files, everything will be done. This is why we recommend you to do the localization of your app exclusively using .m files.
Now you only have to upload your Localizable.strings file to your OnTranslate order and your app will be translated into the languages you choose.
Extracting strings from .xib files
iOS doesn't load automatically the strings from the Lozalizable.strings file to the .xib file. Therefore, the process is a bit more complicated doing it this way. However, it isn't that complicated and it only takes you a few minutes.
We repeat, that the best method to do it, is to define all the strings of your app in .m files. By doing that, we could skip these additional steps.
If we decide to go on, we will have to perform the following 3 steps:
1.- Extract the strings from the .xib files using the tool ibtool
2.- Change the format of this string file and paste it into Localizable.strings
3.- Use the library helper to load the translated string files automatically.
The first step will be to open the terminal and go to the folder en.lproj writing “cd” and dragging the folder “en.lproj” into it.
Once you have done this, you'll have to copy this command:
find . -name \*.xib | xargs -t -I '{}' ibtool --generate-strings-file '{}'.strings '{}'
This will create a file called ViewController.xib.strings, which will look like this:
As you can see, we will have to change the format of the file in order to adapt it to the following:
“This is a text of the interface” = “This is a text of the interface”;
Instead of what is shown:
“jmD-yy-kQc.text” = “This is a text of the interface”;
And also:
“This is another text of the interface” = “This is another text of the interface”;
Instead of:
“xSF-P0-6EA.text” = “This is another text of the interface”;
Copy and paste the content into your file Localizable.strings and remember to edit the previous text as indicated. The complete file should look similar to this:
The next step will be to include this library called “AGi18n”, so that the app looks up the strings of the interface in the file Localizable.strings. This isn't done by default.
Download the .zip file, extract it and move it to your XCode project in the folder “lib”. Make sure that you choose “Copy files to destination group folder” and “Add to targets” after moving the folder.
By doing this, we have finished. Now add your Localizable.strings file to your order on Ontranslate, choose the languages, in which your app should be translated and we will take charge of translating it.
How to integrate translated string files
The first thing you have to do is to download all the string files of your order on Ontranslate. Remember that you can download the whole order in one compressed file once your order has been completed.
Go to the characteristics of your project and add locations for each language your app is translated into.
After that, open the folder of the project in your Finder. There you will find a folder called “.lproj” for each language. For example, if you have added Spanish, you will see a folder called “es.lproj”, if you have added French, “fr.lproj”, etc.
Simply move all folders of your order on Ontranslate to your project, overwrite the folders (now that they have the same names) and you’ve finished.
We recommend you to do a clean-up with “Project > Clean” and to do a display test of some of the languages using the simulator.