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.
The complete code can be found in .
Table of Contents
Creating a project
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.
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.
Check the files that we want to support Chinese. Check Main.storyboard because the Click Me
button is there.
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.
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
.
zh-Hant.xliff
It is a file in XML format. Each string is wrapped by a pair of <trans-unit>
. <source>
is its original language that is English, and <target>
is the translated language that is Chinese. In addition, the comment
parameter of NSLocalizedString
is wrapped by <note>
.
<trans-unit id="PeQ-rk-iei.normalTitle" xml:space="preserve"> <source>Click Me</source> <target>Click Me</target> <note>Class = "UIButton"; normalTitle = "Click Me"; ObjectID = "PeQ-rk-iei";</note> </trans-unit> <trans-unit id="Hello" xml:space="preserve"> <source>Hello</source> <target>Hello</target> <note>Shown by click me</note> </trans-unit>
Let’s replace the strings wrapped by <target>
with Chinese.
<trans-unit id="PeQ-rk-iei.normalTitle" xml:space="preserve"> <source>Click Me</source> <target>點我 </target> <note>Class = "UIButton"; normalTitle = "Click Me"; ObjectID = "PeQ-rk-iei";</note> </trans-unit> <trans-unit id="Hello" xml:space="preserve"> <source>Hello</source> <target>哈囉</target> <note>Shown by click me</note> </trans-unit>
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.
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.
At the bottom right of the preview screen, you can switch the language.
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.
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.