The paper

Designing multilingual content is always a pain in the neck. This paper also found several ways to obtain the current system language.

Be careful to distinguish between the current language in your app and the current language in your system

Swift

let userLanguage   = UserDefaults.standard.object(forKey: "AppleLanguages") // Returns an array
let appLanguages   = Locale.preferredLanguages // Return an array
let deviceLanguage = Locale.current.languageCode
let bundleLanguages = Bundle.main.preferredLocalizations // Return an array

let availableLanguages = Localize.availableLanguages() // The third-party library import Localize_Swift must be introduced to obtain a list of all languages supported by the current app
let currentLanguage = Localize.currentLanguage() // Introduce a third-party library that represents the language currently used by the App
Copy the code

Objective C

NSString *userLanguage   = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"]
NSString *appLanguages   = [NSLocale preferredLanguages];
NSString *deviceLanguage = [[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode];
NSString *bundleLanguages =  [[NSBundle mainBundle] preferredLocalizations]
Copy the code

Form for

userLanguage appLanguages deviceLanguage bundleLanguages availableLanguages
The return type [” useful – Hans – CN “, “en”] [” useful – Hans – CN “, “en”] “Useful” [“zh-Hans”] [” useful – Hans, “” useful – Hant”, “en”]
instructions Retrieved from userDefault, returns an array. Used under the current app The language that the user sets in the phone’s system Settings. Can be achieved bySettings -> General -> Language and RegionSee, not the language the program is displaying. Seems to be a broad category of languages currently in use. No area code Secondary classification with language, but without area code (third-party library support) An array of languages that support switching in the current App setting

;

instructions

  • It can be seen that the accuracy (or granularity) of these methods of obtaining the system language is not the same, the first twouserLanguageandappLanguagesThe obtained code will add the country area code, anddeviceLanguageThe display is only Chinese (category), does not distinguish between traditional Chinese and simplified Chinese, and the last method with the language type of secondary classification
  • Represents the language used by the user in the current app. So it returns an array. This value will return withzh-Hans-CNRegion and Classificationzh-Hant-HK
  • I looked up some materials, but could not find specific documentation for these categories. I’ll keep adding if I find something later.

Sets the current default language

import Localize_Swift

let bundleLanguage = Bundle.main.preferredLocalizations[0]
let availableLanguages = Localize.availableLanguages()
var language = "en" // If no current matching language is found, English is used by default
for availableLanguage in availableLanguages {
    if let _ = availableLanguage.range(of: bundleLanguage) {
        language = appLanguage
        break}}Copy the code