iOS localization

50 %
50 %
Information about iOS localization

Published on July 14, 2016

Author: LammertWesterhoff

Source: slideshare.net

1. LOCALIZING YOUR APP INTERNATIONALIZATION JULY 13, 2016 By Lammert Westerhoff

2. TYPE A QUOTE HERE. LAMMERT WESTERHOFF

3. LOCALIZING YOUR APP WHAT I’LL BE TALKING ABOUT ▸ Basics of String localization ▸ Storyboard localization ▸ Automating the update process ▸ Generating custom keys ▸ Formatting Strings with arguments ▸ String dictionaries

4. LOCALIZING YOUR APP WHAT I’LL NOT BE TALKING ABOUT ▸ RTL (Right-to-left) Languages ▸ Localized images ▸ Localized Info.plist (App name) ▸ XLIFF files

5. LOCALIZING YOUR APP PROJECT SETTINGS First of all, add the languages you need to support in your project settings (not target settings).

6. LOCALIZING YOUR APP BASICS OF STRING LOCALIZATION Instead of hard coded Strings, always use NSLocalizedString with a meaningful comment. This will use Strings defined in the Localizable.strings file. You’ll have one for each language you support. buyButton.setTitle(NSLocalizedString("Buy", comment: "Title for the button to buy an item from the store"), forState: .Normal) "Buy" = "Koop";

7. LOCALIZING YOUR APP RUN IN DIFFERENT LANGUAGE You can control the Application Language in your scheme options:

8. LOCALIZING YOUR APP LOCALIZE STORYBOARDS You can localize a Storyboard which will generate a .strings file for that Storyboard. Also works for xib files. /* Class = "UIButton"; normalTitle = "Buy"; ObjectID = "7Vf-Sa-WMo"; */ "7Vf-Sa-WMo.normalTitle" = "Koop";

9. DEMO

10. LOCALIZING YOUR APP XCODE TOOLS ▸ genstrings to generate .strings files from NSLocalizedString in code ▸ ibtool to generate .strings from Storyboards ▸ appleglot ▸ “Export For Localization…” WHICH ONE TO USE?!

11. LOCALIZING YOUR APP LET’S AUTOMATE EVERYTHING! Solution: mergegenstrings.py It’s a script you add to your build phase that handles everything for you. https://gist.github.com/yoichitgy/29bdd71c3556c2055cc0

12. DEMO

13. LOCALIZING YOUR APP NEW PROBLEM Now that we’re automating everything, we can’t add custom strings to Localizable.strings anymore. These entries will be replaced by the script: for day in 0 ..< 7 { print(NSLocalizedString("day(day)", comment: "One of the days of the week")) } "day0" = "Monday"; "day1" = "Tuesday"; /* One of the days of the week */ "day(day)" = "day(day)";

14. LOCALIZING YOUR APP SOLUTION ▸ Place in another .strings file. ▸ E.g. CustomLocalizable.strings ▸ Don’t use NSLocalizedString (else the script finds it) func CustomLocalizedString(key: String) -> String { return NSBundle.mainBundle().localizedStringForKey(key, value: key, table: "CustomLocalizable") } for day in 0 ..< 7 { print(CustomLocalizedString("day(day)") }

15. DEMO

16. LOCALIZING YOUR APP FORMATTING STRINGS WITH ARGUMENTS let place = "CocoaheadsNL Meetup" let when = NSDateFormatter.localizedStringFromDate(NSDate(), dateStyle: .LongStyle, timeStyle: .NoStyle) let format = NSLocalizedString("Let's go to %@ on %@", comment: "Printing where we go at which date") print(String.localizedStringWithFormat(format, place, when)) Generates: We can change the order of the arguments: "Let's go to %@ on %@" = "Laten we op %2$@ naar %1$@ gaan"; "Let's go to %@ on %@" = "Let's go to %1$@ on %2$@";

17. LOCALIZING YOUR APP USE STRING DICTIONARIES FOR PLURALISATION For example, to display the number of transfers of a flight. No transfers: “Direct” 1 transfer: “One transfer” More transfers: “2 transfers” String.localizedStringWithFormat( NSLocalizedString("%d transfers”, comment: ""), transfers) Instead of adding it to the .strings file, we’ll create a .stringsdict file.

18. LOCALIZING YOUR APP USE STRING DICTIONARIES FOR PLURALISATION String.localizedStringWithFormat( NSLocalizedString("%d transfers”, comment: ""), transfers)

19. DEMO

20. LOCALIZING YOUR APP SOURCE CODE: HTTPS://GITHUB.COM/LAMMERTW/LOCALIZATIONDEMO FOLLOW ME ON TWITTER: @LWESTERHOFF Related: https://www.objc.io/issues/9-strings/string-localization/

Add a comment