We don’t need to create language files by ourselves because Xcode helps us generate language files. We just need to fill in translations in language files and import them into Xcode.
Normally, Apps support localizations. It’s easy to implement localizations in iOS. We don’t need to create language files by ourselves because Xcode helps us generate language files. We just need to fill in translations in language files, import them into Xcode. This article will introduce how to implement localization with Xcode.
First, let’s build a simple App. In this App, click the Click Me button as shown below, and it will be replaced with Hello. Next, we make Click Me and Hello support Chinese.
iOS Localization Example
class ViewController: UIViewController {
@IBOutlet weak var label: UILabel!
@IBAction func onClick(_ sender: Any) {
label.text = "Hello"
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
Adding Chinese Localization in Xcode
Generally, after the project is created, the default is English. Let’s add Chinese to it.
Add Chinese
Check the files that we want to support Chinese. Check Main.storyboard because the Click Me button is there.
Select Chinese localization file
After that, you can see Chinese, Traditional are added under Localizations. In addition, a file named Main.strings is created under Main.storyboard, and it also indicates that it is for Chinese.
This Main.strings is a localization file of Main.storyboard for Chinese. However, we will not directly add a Chinese translation here, because Xcode provides a more convenient way.
Xcode Localizations
NSLocalizedString
NSLocalizedString returns localized strings according to App’s locale. So, we need to use NSLocalizedString to get strings.
class ViewController: UIViewController {
@IBOutlet weak var label: UILabel!
@IBAction func onClick(_ sender: Any) {
label.text = NSLocalizedString("Hello", comment: "Shown by click me")
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
Xliff file
Now, let’s add Chinese translation for all strings. First, export the localization file. After that, find .xliff file under the exported directory. As we support English and Chinese languages, you should find en.xliff and zh-Hant.xliff. Because we want to add Chinese translation, let’s open zh-Hant.xliff.
Export localizations
zh-Hant.xliffIt is a file in XML format. Each string is wrapped by a pair of . is its original language that is English, and is the translated language that is Chinese. In addition, the comment parameter of NSLocalizedString is wrapped by .
Click MeClick MeClass = "UIButton"; normalTitle = "Click Me"; ObjectID = "PeQ-rk-iei";HelloHelloShown by click me
Let’s replace the strings wrapped by with Chinese.
Click Me點我 Class = "UIButton"; normalTitle = "Click Me"; ObjectID = "PeQ-rk-iei";Hello哈囉Shown by click me
Now, let’s import zh-Hant.xcloc folder. After the import, Localizable.strings file will be created by Xcode. NSLocalizedString finds translation strings by this file.
Import localizations
When you need to translate a lot of strings, it becomes very complicated to edit .xliff files directly. There are many free online Xliff editors.
Preview Storyboard
After we import the Chinese localization file, we can preview Storyboard. Click Adjust Editor Options -> Preview in the upper right corner.
Enable Preview
At the bottom right of the preview screen, you can switch the language.
Preview Storyboard
Running the App
When the project is run on the simulator, the English localization is set by default. We can let the App run in Chinese without changing the system language of the simulator.
Click Edit SchemeSwitch App language
Conclusion
In Xcode, we can write strings anywhere. When exporting language files, these strings will be included. We don’t need to make language files ourselves. There are many development tools that do not support this feature. Although creating language files is not difficult, it is very cumbersome to maintain.
Grand Central Dispatch (GCD) provides efficient concurrent processing so that we don’t need to directly manage multiple threads. Its dispatch queues can execute tasks serially or concurrently.
Swift packages are reusable code components. It can contain not only code, but also binary XCFrameworks. Distributing XCFrameworks can protect source code.