Since Xcode 6, it is possible to export strings in the app to an XLIFF file for translation, which makes localization easier. Here is how to do it:
Get your project ready for localization
Prepare your code
To get your code ready for localization, you will need to use the
NSLocalizedString macro like the following:
// Instead of this NSString *title = @"Hello"; // Do this NSString *title = NSLocalizedString(@"Hello", @"title in alert");
// Instead of this let title = "Hello" // Do this let title = NSLocalizedString("Hello", comment: "title in alert")
The second argument is called "comment", it has no actual effect, but it provides hints for translators. So it is better to fill in something about the context. It will be useful later.
Get your resource files ready
After modifying your code, the next step is resource files such as xib and storyboard.
Select your project from the sidebar, click
Infoand add a new language by clicking the little
Select resource files that you would like to localize. For example interface files.
If you want to add new languages to a existing file, you can click the
Localize...button in the utility sidebar. Then select the supported languages.
Export XLIFF file
After getting your project ready, the next step would be export the strings so they can be translated.
- Select the project at the sidebar (not target)
Export For Localization...
- Translate strings in the XLIFF files.
Edit the XLIFF file
There are few apps and sites that can open and edit XLIFF files, here I will use Xliffie as an example.
You may notice there are some info at the sidebar telling you it is an
UILabel, they are comments generated by Xcode.
Take care of printf formatted string
Printf-formatted strings need special care, it may causes bugs or even app crashes if they are not properly handled. For example translating a
%d will causes the app to crash.
Using translation services
When there are lots of strings in your app, it maybe a time consuming task to translate all of them and some people prefer using translation services like Google / Bing Translate to localize some simple phrases. It worths noting that some languages doesn't use the same symbol as English, and may cause problem with printf-formatted string. For example
% is translated to
％ in Chinese, which is a full-width character instead of half-width.
Import the XLIFF file
After editing the XLIFF file, it is time to apply it back to your project.
Import For Localization...
Confirm the changes
Done, Your app is translated!
Xliffie is an XLIFF editor designed for developers, it integrates Google / Bing Translate, supports printf-formatted strings, and you can search with regular expression too!