Localisation in iOS
Why you need localisation?
Many people do NOT speak English.
English is Second most used language after Mandarin. Wikipedia
So by developing you app for only English (the usual development language), you are not reaching even the largest set of people in this world.
Localising an app makes it available to a larger number of people. It can be easily made available across borders. New markets, more $$$.
Process:
iOS has very good documentation for everything, including localisation.
Overview:
- Use NSLocalizedString to add all your UI strings to localisation system.
- Run genstrings on your files to create
.stringsfile for your strings that need to be translated. - Send this
.stringsfile to your translator, and they will give you back the translated.stringsfile. - Add the received files to your project.
- Count 💰💰💰
But I got xibs/storyboards!
- Select your xib/storyboard
- Open the
Utilitiespane for the said file. - In the
File Inspector, go toLocalizationand add the languages you want to support.
The only issue we faced was the location of these files. When you localise them, their
.stringsis generated in same location as the xib/storyboard. Keeping track becomes tricky.
Note:
Don’t use NSAttributedString inside a xib or storyboard.
These strings can not be localised using genstrings (since they are inside a xib/storyboard). Also, to localise an attributed string you need to make sure that the attributes are applied to correct parts of the translated text too. Hence it is always better to create such strings in code, where you get more control. You can then apply attributes to text, making the text language.
If you have a larger text, split it up and localise the parts individually. This way, you can apply attributes to specific ranges.
Advanced:
- Add build step to run genstrings on all your implementation (
.m) files. This will generate theLocalizable.stringsfile for you each time.- Pro: Whenever there is a change in your strings, your version control will mark the file dirty.
1
find **SOURCE_FOLDERS** -name \*.m | xargs genstrings -o Resources/en.lproj/
-
Create a zip of all your
.stringsto be given to your translator auto-magically1
zip -rj Resources/iOS-strings Resources/en.lproj/*
- Though most of the
UIKitviews (UILabel, UITextView etc.) work good with localised content, but an edge case is when you are setting theframeyourself. You need to calculate the frame according to the content each time instead of using constants. #Gotcha-
Wrong:
1
CGRect frame = CGRectMake(0, 0, 100, 100);
-
Correct:
1 2 3 4
CGRect rect = [self boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName: font} context:nil];
-
-
Use pseudolocalisations in development phase to figure out layout issues early. Pseudolocalisations are available for interface builders. They show you a preview of the screen with localised strings of the chosen laguange.