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.
Install the Resource Bundle Editor plugin
Install the Resource Bundle Editor plugin to be able to work with properties:
In the Settings dialog Ctrl+Alt+S, select Plugins.
Switch to the Marketplace tab and type
Resource Bundle Editor
in the search bar.Click Install next to the plugin name, apply the changes and close the dialog.
AppCode provides coding assistance features that simplify software internationalization and localization processes.
Strings files
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:
Select
from 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 NSLocalizedString
macro.
In the editor, place the caret at the string literal that you want to localize.
Press Alt+Enter, select Localize string, and press Enter.
AppCode will replace the string literal with the
NSLocalizedString
macro 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
You can find and add missing localization strings using the dedicated inspections and intention actions.
Find all missing localization strings
Press Ctrl+Alt+Shift+I or select
in the main menu.In the search box, start typing the inspection name or its first letters: Missing localization (Swift) or String localization (Objective-C) and select it from the list:
If needed, specify the inspection scope and the file mask. If you want to get warnings only when a localization key is missing in all available strings files (for example, to make sure that the key is defined at least for the development language), enable the Report only localizations missing in all languages option:
From the Problems 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
NSLocalizedString
macro or at any string in a strings file and press Alt+Enter.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 Alt+Enter.
Select Safe delete localized string '<string>' and press Enter:
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.
From the main menu, select Edit Configurations from the selector on the run/debug toolbar.
or chooseIn the dialog that opens, select your run/debug configuration or create a new one.
Open the Options tab and select a language and region in the corresponding fields.