Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get OS language and locale?

My app changes the language at runtime and advises the user to restart the app. I do it like this:

    typealias LanguageLocaleType = (language: String?, locale: String?, title: String)

    let elements: [LanguageLocaleType] = [
        (nil, nil, "System Default"),
        ("en", "en_US", "English"),
        ("ar", "ar_SA", "Arabic"),
    ]

    //...func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)...

    let element = elements[indexPath.row]

    guard let language = element.language else {
        // How to really reset app back to OS language/locale???
        UserDefaults.standard.removeObject(forKey: "AppleLanguages")
        UserDefaults.standard.removeObject(forKey: "AppleLocale")
        return
    }

    UserDefaults.standard.set([language], forKey: "AppleLanguages")

    if let locale = element.locale, !locale.isEmpty,
        Locale.current.identifier != locale,
        !Locale.current.identifier.hasPrefix("\(language)_") {
            UserDefaults.standard.set(locale, forKey: "AppleLocale")
    }

I want to offer to set languages in a list with the right one selected, one of which being offered it to set language back to System Default. However, there's no way to find the OS-level language and locale that I could find. Since after I mess around with setting the UserDefaults, Bundle.main.preferredLocalizations is not reliable and do not match the system default (event when I remove the key from the User Default).

Is there a way to get the OS-level language and locale instead of the app-level?

like image 866
TruMan1 Avatar asked Jun 30 '17 21:06

TruMan1


People also ask

How do I find my locale OS?

Find the System Locale with the System Information appPress the Win + R hotkeys together on the keyboard and type the following command in your Run box: msinfo32 . Click the System Summary section on the left. On the right, see the Locale value.

What is locale OS?

The locale setting of your operating system session determines the language of the user interface and the globalization behavior for components such as Oracle Universal Installer, Oracle Net Configuration Assistant, and Oracle Database Configuration Assistant.

How do I get locale on my browser?

To get the user's locale in the browser, access the first element of the languages property on the navigator object, e.g. navigator. languages[0] . The property returns an array of strings that represent the user's preferred languages.

What is system locale language?

SystemLocale specifies the default language to use for non-Unicode programs. This setting is used by both Windows Setup and Windows Deployment Services. The system locale specifies which bitmap fonts and code pages (for example, ANSI or DOS) are used on the system by default.


1 Answers

You should be able to get currently selected OS language like this.

    let language  = NSLocale.preferredLanguages[0]

if this is not what you are looking for, refer to this answer https://stackoverflow.com/a/30750120/809821

like image 182
Rukshan Avatar answered Oct 19 '22 14:10

Rukshan