Internationalization and localization
Internationalization (i18n) refers to the process of extracting strings from source code and presenting them as properties with a set of values.
Localization (l10n) is the process of translating values of these properties into target languages. Also, localization refers to the set of resources (texts, images, icons, audio, and so on) for a specified language and region.
AppCode provides coding assistance features that simplify software internationalization and localization processes.
Strings that need to be localized are stored in the text files with the .strings extension, so called strings files. Strings files contain key-value pairs where the left part is a key, and the right part is a user-facing string that should be translated:
The strings files reside in the .lproj directories of an Xcode project, for example, en.lproj or es.lproj. In the Project view of the Project tool window, strings files are shown as a group of language-specific resources, whereas in the File view you can see their actual location:
|Project view||Files view|
Create a strings file
To add a new strings file to your project, do one of the following:
Selectfrom the main menu and in the dialog that opens specify the file name and location.
Use the Localize string intention action as described below.
After a strings file is created, you should add it to different localizations from Xcode. For more details, refer to the Xcode documentation.
Extract values to a strings file
In AppCode, you can easily replace hard-coded string literals with the
In the editor, place the caret at the string literal that you want to localize.
Press ⌥⏎, select Localize string, and press ⏎.
AppCode will replace the string literal with the
NSLocalizedStringmacro and add this string to the Localizable.string file for all available localizations. If there is no file with such a name in your project, AppCode will suggest you to create a new one:
Add missing localization strings
Find all missing localization strings
Press ⌃⌥⇧I or selectin the main menu.
In the search box, start typing the inspection name — Missing localization language (Swift) or String localization (Objective-C) — and select it from the list:
If needed, specify the inspection scope and the file mask and press OK:
From the Inspection Results tool window that opens add all missing strings by clicking the Provide localization for '<string>' button:
Add missing localization strings with intention action
Place the caret at the first parameter of the
NSLocalizedStringmacro or at any string in a strings file and press ⌥⏎.
If the selected string is missing in any of available localization files, the Provide localization for '<string>' action will be available. Select it in the list to add all the missing strings:
Delete unused localization strings
Unused strings are grayed out in the strings file.
To delete such strings, do the following:
Place the caret at the grayed out string and press ⌥⏎.
Select Safe delete localized string '<string>' and press ⏎:
In the dialog that opens, choose if you want to search for the string usages in comments and text contents and click OK. If any usages are found, the Find tool window will appear. Check the search results and click Do Refactor to delete unused strings.
Change the application language and region
You can change the application language and region at runtime to test different localizations of your app.