The application of NSLocale. PreferredLanguages

When we develop iOS apps, there is often a need to differentiate using the language of the current app Settings.

For example, when you develop a browser and need to push web articles to users in different languages, depending on their language Settings. This time will need to use NSLocale preferredLanguages.

Generally, application developers only need to distinguish Chinese and English to complete the basic requirements, such as the following code:

if NSLocale.preferredLanguages[0].hasPrefix("en") {
    // Branch logic
}
Copy the code

And if you try to print NSLocale preferredLanguages, use the following code

print("\(NSLocale.preferredLanguages)")
Copy the code

You will get an array. The example result is as follows:

["en-HK", "zh-Hans-HK", "zh-Hant-HK", "zh-Hant-TW", "yue-Hant-HK"]
Copy the code

Meaning of an array

Why NSLocale. PreferredLanguages return result is an array, is not as long as the returns an en – HK will be enough?

The purpose of this function is to provide the local device language preferences. The meaning of providing an array is that the system returns multiple prioritized language-locale Settings based on the user’s local information and the user’s system Settings. This allows you to select possible languages from the list if the highest-priority language application is not supported.

What is en-HK?

PreferredLanguages indicates English, and HK indicates Hong Kong.

That’s a good guess, but what strict standards the code follows and how we should write it if we want to support multiple languages.

First, en. En is used for a language, such as EN in English or en in Chinese. This code is called the Global Language Standard Code-ISO-639.

The language code of ISO-639 is further subdivided, and the subsequent subdivisions are represented by numerical codes, which Apple chose as ISO-639-1 and ISO-639-2. Iso-639-1 marks a language by two letters of English, and ISO-639-2 by three letters.

The following is an example of the ISO-639-1 and ISO-639-2 codes for each language:

language ISO-639-1 ISO-639-2
English en eng
French fr fre
German de ger
Japanese ja jpn

PreferredLanguages returns a unit where en is the English code in iso-639-1.

However, because the ISO-639-1 notation does not exhaust all languages, for example, for Hawaiian, the corresponding code cannot be found in ISO-639-1, and it can be found in ISO-639-2, which is haw.

So Apple’s language code returns the code found in ISO-639-1 first, if not, returns the code found in ISO-639-2.

Similarly, the symbol HK belongs to the region code, which adopts ISO 3166-1 and is an internationally recognized code established by countries and regions.

The following is an example of ISO 3166-1 coding:

region ISO 3166-1
Chinese mainland CN
The United States US
Britain GB
The French FR

Literal encoding

En -hk: En -hk: en-hk: en-hk: en-hk: en-hk: en-hk: en-hk: en-hk: en-hk: en-hk: en-hk: en-hk: en-hk: en-hk: en-hk: en-hk: en-hk: en-hk: en-hk: en-hk: en-hk: en-hk: en-hk: en-hk: en-hk

This comes to the word coding, for example, we Chinese, even if the same written mandarin characters, also divided into simplified Chinese and traditional Chinese.

Therefore, in the returned encoding results, if the literal system does not distinguish and doubt, such as English, then do not need to append the text encoding, and if there is subdivided, will append the text encoding. Word coding is also regulated by ISO standardization Organization.

The following is an example of simplified Chinese and traditional Chinese character encoding:

The text coding
Simplified Chinese Hans
Traditional Chinese Hant

So, in example zh-hant-hk, we can conclude that the returned result is Chinese – traditional – Hong Kong.

Other Notes

Some people may see this, when writing code will default the element code returned by preferredLanguages must have a -, the fact is not the case, sometimes the return result may just be an en, no locale code.

. In other words, when we get NSLocale preferredLanguages, there are three kinds of the returned array element value possible, specific can be expressed in the following three values:

en
en-hk
zh-Hant-HK
Copy the code

When only the language is returned, the developer can choose the locale of the language as the default option. Specifically, English can default to the United States, Chinese simplified default to mainland China, and so on.

Write a simple class that encapsulates the locale code

Let’s end this article with a class that accepts elements from the preferredLanguages array.

class LanguageRegionCode {
    private var _languageCode: String?
    private var _scriptCode: String?
    private var _regionCode: String?
    
    var languageCode: String? {
        _languageCode
    }
    
    var scriptCode: String? {
        _scriptCode
    }
    
    var regionCode: String? {
        _regionCode
    }
    
    init(code: String) {
        let splits = code.split(separator: "-")
        if let lCode = splits.first {
            _languageCode = String(lCode)
        }
        if splits.count = = 3 {
            _scriptCode = String(splits[1])
            _regionCode = String(splits[2])}else if splits.count = = 2 {
            _regionCode = String(splits[1])}}}Copy the code

The code for using this class is as follows:

let code = NSLocale.preferredLanguages[0]
let languageRegionCode = LanguageRegionCode(code: code)

if let languageCode = languageRegionCode.languageCode {
    print("language code = \(languageCode)")}if let scriptCode = languageRegionCode.scriptCode {
    print("script code = \(scriptCode)")}if let regionCode = languageRegionCode.regionCode {
    print("region code = \(regionCode)")}Copy the code

For zh-hex-hk, the output is as follows:

language code = zh
script code = Hant
region code = HK
Copy the code

Resources: apple NSLocale preferredLanguages official document: developer.apple.com/documentati… Apple’s technical documentation: developer.apple.com/library/arc…


Friends who want to discuss together can apply for adding group in the group menu bar of my public account Fenghai Causeway to complete adding group application, and make progress together.