Skip to content

Localization

Daniel Jilg edited this page Nov 22, 2019 · 7 revisions

Intro

Localization in general works as described in the Apple Documentation or this helpful tutorial. Strings files are included in the project and can be exported to and imported from Xliff files if necessary for translation by external translation agencies.

Strings files live in the Translations directory,

To test localization, you can edit your currently active scheme, and in "Options", set the "Application Language". Don't commit this change please.

Adding new Strings

1. Decide where your new Strings should live

There is a Localizable.strings file, but there are also files for individual subsystems of the app. Consider adding your strings to a fitting subsystem if possible.

If you put your strings in the Localizable.strings file, you should add references to them into Strings.swift. If you put them in another file, say MyOtherStringsFile.strings, you should add references to them into Strings+MyOtherStringsFile.swift.

2. Add to Strings.swift

Do not put NSLocalizedString instances directly into your code please. Instead, add the to Strings.swift or an extension thereof. Check if there is already a section there where your strings fit in well, and if not add one. Sections should look like below. Don't forget the tableName parameter.

Localized Strings should use keys that describe a path to where they can be found, like HomePanel.ContextMenu.OpenInNewTab. Do not use "Open in New Tab" as a key!

public struct Strings {
    // MARK: - Search
    public struct Search {
        public struct ThirdPartyEngines {
            public static let AddSuccess = NSLocalizedString("Search.ThirdPartyEngines.AddSuccess", comment: "")
            public static let AddTitle = NSLocalizedString("Search.ThirdPartyEngines.AddTitle", comment: "")
        }
        // ...
    }

    // MARK: - Bookmarks
    public struct Bookmarks {
        public static let Title = NSLocalizedString("Bookmarks.Title" comment: "The label for the title of a bookmark")
    }
}

Note that keys should be the same in both the strings file and in swift code. That means that if the key in the strings file is Search.ThirdPartyEngines.AddSuccess, then I should be able to access it via Swift as Strings.Search.ThirdPartyEngines.AddSuccess.