AppCode 2020.3 Help

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 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:

"Create" = "Create a new project"; "Remove" = "Remove from the list"; "Close" = "Close";

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
Strings files in Project view
Strings files in Files view

Create a strings file

To add a new strings file to your project, do one of the following:

  • Select File | New | Strings File 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.

  1. In the editor, place the caret at the string literal that you want to localize.

  2. Press ⌥⏎ , select Localize string, and press .

    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:

    Localize string intention action

Add missing localization strings

You can find and add missing localization strings using the dedicated inspections and intention actions.

Find all missing localization strings

  1. Press ⌃⌥⇧I or select Code | Run Inspection By Name in the main menu.

  2. In the search box, start typing the inspection name — Missing localization language (Swift) or String localization (Objective-C) — and select it from the list:

    Find inspection by name

  3. If needed, specify the inspection scope and the file mask and press OK:

    Define inspection scope

  4. From the Inspection Results tool window that opens add all missing strings by clicking the Provide localization for '<string>' button:

    Missing languages inspection results

Add missing localization strings with intention action

  1. Place the caret at the first parameter of the NSLocalizedString macro or at any string in a strings file and press ⌥⏎ .

  2. 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:

    Provide localization intention action

Delete unused localization strings

Unused strings are grayed out in the strings file.

To delete such strings, do the following:

  1. Place the caret at the grayed out string and press ⌥⏎ .

  2. Select Safe delete localized string '<string>' and press :

    Delete localized string quick-fix

  3. 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.

  1. From the main menu, select Run | Edit Configurations or choose Edit Configurations from the selector on the run/debug toolbar.

  2. In the dialog that opens, select your run/debug configuration or create a new one.

  3. Open the Options tab and select a language and region in the corresponding fields.

    Change application language

Last modified: 30 October 2020