Localization is the translation of the app’s user interface into locale specific languages. In order to support localization we will use a strings file.
Creating a Localizable.strings file
I like to place the strings file in its own folder called “Resources”.
In Xcode, Select “File”, “New”, “File…”
In the template picker select the “Resource” for the appropriate OS. Select “Strings File”.
Name the file “Localizable.strings”. This is the iOS default naming convention, don’t name it something else unless you want to refer to it by name every time you need to reference it.
Place it in the Resources Group or whichever Folder your prefer and press “Create”.
The Localizable.strings file follows the following format: “KEY” = “Content”;
So if you had a label in the Settings tab of your app that needed to read Contact Us you might use the following key, value pair:
//Settings "SETTINGS_CONTACT_US" = "Contact Us";
You would then refer to it in code as:
label.text = NSLocalizedString("SETTINGS_CONTACT_US", comment: "")
label.text = NSLocalizedString(@"SETTINGS_CONTACT_US", nil)
To add a string with a parameter in Swift use a %@ sign in the value of the .strings file and in the code use String.localizedStringWithFormat. Below is an example:
Localizable.strings //Hello Planet "GREETINGS" = "Hello %@"; ... swiftClass.swift ... //The label will be "Hello Earth" let planetsName = "Earth" let label = UILabel() label.text = String.localizedStringWithFormat(NSLocalizedString("GREETINGS", comment: ""), planetsName)
Adding new languages
To add a new language in Xcode select the Localizable.strings file in the Project Navigator. From the utilities pane on the right, in the File Inspector under the Localization section, press the “Localize…” button.
You will be asked “Do you want to localize this file? The file will be moved into the lproj folder for the following language. Base.” Select Localize.
From the Project Navigator select your project. Then select the Project, Info tab. Under the Localizations sections press the + sign to add a new language. Repeat for every language you want to support.
If you are using Storyboards then each storyboard will also have it’s own Strings file. Make sure the Localizable.strings file is selected. In the Project Navigator you will see that the Localizable.strings file can be expanded and there are now new versions for every language you have added. Add the translations to the new Strings file.
If you need to localize your App Name as it appears on the home screen then check out this post on How to localize an app’s name with an InfoPlist.strings file.
Did you find this post helpful? Support independent development and improve your sleep by downloading White Noise and Deep Sleep Sounds free from the App Store.
Keyword level attribution for Apple Search Ads. Optimize your bids and increase your revenue. Sign up for free at kitemetrics.com.