iOS Localization

Photo by Kevin Walker on Unsplash
Photo by Kevin Walker on Unsplash
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.

The complete code can be found in .

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.

iOS Localization Example
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
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
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
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
Export localizations

zh-Hant.xliffIt 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.

Import localizations
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
Enable Preview

At the bottom right of the preview screen, you can switch the language.

Preview Storyboard
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 Scheme
Click Edit Scheme
Switch App language
Switch 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.

Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like
Photo by Alex Alvarez on Unsplash
Read More

Dispatch Queue Tutorial

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.
Read More