An app that supports localization will likely need to replace the following to adjust to different languages:
Localization works by creating multiple files to store the text you want to display in other languages. To create localization files, you need to define which languages you want your Xcode project to support. For each language you want your app to support, you’ll need to define one localization setting.
To see how to add localization to a project, follow these steps:
The Main.storyboard (Base) file contains the storyboard of your project where you can design the user interface. The Main.strings file contains text to display on the user interface. Xcode identifies the text to appear in the user interface by the Object ID, which appears on the Identity Inspector pane.
To view the Object ID of a user interface object, follow these steps:
Storing text
The most common way to store localized text in an app looks like
var greeting = NSLocalizedString("Hello", comment: "Formal greeting")
The comment
portion of the NSLocalizedString
is optional, but is meant to help a translator understand the context of the text. This can help a translator more accurately translate your text based on the comment you provide.
Once you’ve identified text in your code as NSLocalizedString
, the next step is to edit the Main.strings file that shows the user interface (identified by Object ID) and its equivalent word or term in another language.
In our example, let’s assume that we want to display a greeting in a label. That means we’ll need both our native language text and the foreign language text to appear in a label, identified by its Object ID.
When we define a localization file (such as Ukraine), Xcode automatically creates a Main.strings file that identifies user interface objects by their Object ID such as
/* Class = "UILabel"; text = "Hello"; ObjectID = "X1f-Wi-Yee"; */ "X1f-Wi-Yee.text" = "Привiт";
What we need to do is customize this Main.strings file to display the foreign language equivalent. To do this follow these steps:
Edit the viewDidLoad
method as follows:
override func viewDidLoad() { super.viewDidLoad() greetingLabel.text = NSLocalizedString("Hello", comment: "Formal greeting") }
Creating a Localized String File
At this point if you run the app, it will always display the text "Hello" in the label that appear on the user interface regardless of the iOS device’s language preference. That’s because even though we defined what Ukraine text to appear on the user interface through the Main.strings (Ukraine) file, we still need to define what strings to appear when the app actually runs. The Main.strings (Ukraine) file just lets us preview our user interface with different languages, but does not define which foreign language words to use while the app runs.
To do this, we need to define each text to replace everywhere we defined text as NSLocalizedString
. That means we need to follow these steps:
NSLocalizedString
code to define placeholder text to appear.To see how to create localized string files for each foreign language you want to support, follow these steps:
At this point, both .strings files are empty. What we need to do is go through our code, put placeholder text in all NSLocalizedStrings
, and define what actual text we want to appear for each placeholder text. The names we give our placeholder text can be any arbitrary text as long as it’s distinct and unique. For our example, we’ll use the following placeholder text:
greeting_text
To see how to display text in foreign languages in our app, follow these steps:
viewDidLoad
method as follows:greetingLabel.text = NSLocalizedString("greeting_text", comment: "Formal greeting")
Click the Localizable.strings (English) file in the Navigator pane. Add the following inside the Localizable.strings (English) file:
"greeting_text" = "Hello";
Click the Localizable.strings (Ukraine) file in the Navigator pane. Add the following inside the Localizable.strings (Ukraine) file:
"greeting_text" = "Привiт";
Now that we’ve defined placeholder text along with English and Ukraine words to appear when the app runs, it’s time to test the app as if the iOS Simulator were running in a different language.
Localizing Images
To localize text, we needed to insert placeholder text in our code. Then we had to create two separate Localizable.strings files where each .strings file contained both the placeholder text we used and its actual text we want the app to use for different languages such as
"greeting_text" = "Hello";
Localizing images is no different except you use placeholder text to specify a file name to display. Then you need a different image for each language such as an image for English and a different image for Ukraine such as
let imageFile = NSLocalizedString("flag_file", comment: "National flag") myImageView.image = UIImage(named: imageFile)
In each language’s Localizable.strings file, you need to specify the exact file name such as
"flag_file" = "usaFlag";
Now you just need an image named usaFlag
in your project. For the purposes of this project, we’ll assume the American flag image is called usaFlag.png and the Ukraine flag is called ukFlag.png.
Drag both flag images into your project’s Navigator pane. When a dialog appears, click Finish button. Xcode should now display the two flag images in the Navigator pane.
Customizing the App Name
One final step to localization is customizing the app name. To do this, you need to create a separate InfoPlist.strings file for each language you want to support. Then in each InfoPlist.strings file, you define the CFBundleDisplayName value such as
"CFBundleDisplayName" = "App Name";
Whatever name you define here is what appears underneath the app’s icon when it appears on the Home screen. To see how to localize the name for your app, follow these steps:
"CFBundleName" = "$(PRODUCT_NAME)"; "CFBundleDisplayName" = "USA App";
"CFBundleDisplayName" = "Ukraine App";
Formatting Numbers and Dates
Every region tends to display numbers and dates in different ways. To make your app format data such as numbers and dates based on the user’s language and region, use Apple’s various Formatters such as NumberFormatter or DateFormatter. Apple’s various formatters can automatically adjust the appearance of data based on the iOS device’s language and region. Your app just needs to calculate the data to appear on the user interface.
The basic step to using a formatter involves choosing which formatter to use such as
let formatter = DateFormatter() let formatter2 = NumberFormatter()
Then define one or more settings for how to format the information such as
formatter.dateStyle = .full formatter2.numberStyle = .currency
Finally, use the formatter to convert the data such as
let myDate = formatter.string(from: Date()) let myMoney = formatter2.string(from: 123456) dateLabel.text = NSLocalizedString("\(myDate)", comment: "Date format") numberLabel.text = NSLocalizedString("\(myMoney!)", comment: "Number format")
To see the differences in how the formatters work, we need to run our app as if we’re in a different region of the world. So not only can we set the language for the Simulator to use, but we can also define the region for the Simulator to mimic.
Useful links