Getting current device language in iOS?

Viewed 259031

I'd like to show the current language that the device UI is using. What code would I use?

I want this as an NSString in fully spelled out format. (Not @"en_US")

EDIT: For those driving on by, there are a ton of useful comments here, as the answer has evolved with new iOS releases.

33 Answers

iOS13, Swift 5+, WWDC2019 https://developer.apple.com/videos/play/wwdc2019/403/

Users can select the preferred language of an app independently from the OS language.

You can use these:

    // Returns a list of the user's preferred languages.
    // Maybe more than (or none of) your app supports!
    Locale.preferredLanguages

    // a subset of this bundle's localizations, re-ordered into the preferred order
    // for this process's current execution environment; the main bundle's preferred localizations
    // indicate the language (of text) the user is most likely seeing in the UI
    Bundle.main.preferredLocalizations



    // The current running app language
    Bundle.main.preferredLocalizations.first

    // list of language names this bundle appears to be localized to
    Bundle.main.localizations

For MonoTouch C# developers use:

NSLocale.PreferredLanguages.FirstOrDefault() ?? "en"

Note: I know this was an iOS question, but as I am a MonoTouch developer, the answer on this page led me in the right direction and I thought I'd share the results.

In Swift:

let languageCode = NSLocale.currentLocale().objectForKey(NSLocaleLanguageCode) as? String

In Swift 4.2 and Xcode 10.1

let language = NSLocale.preferredLanguages[0]
debugPrint(language)//en

In Swift 5.x

let langStr = Locale.current.languageCode
debugPrint(langStr ?? "") //en  el

I actually misread the original question, thought it asked for the "app UI" language (that's what I had googled for), not the "device UI", in which case the best answers would be the ones using preferredLocalizations, but those answers still give you a code, there is one more step to get a nice string to display. So, while the "device UI" language is already answered, if you want to display a nice string for which of the UI languages you support is currently in use, obviously the simplest solution is:

NSLocalizedString(@"currentLanguage", @"")

Where in every one of your UI localizations you have specified it exactly the way you want it shown. E.g. in the en version of your .strings file you'd have:

"currentLanguage"="English";

in your fr version of the .strings file you'd have:

"currentLanguage"="Francais";

etc. No messing with codes etc, you make your own strings to nicely match your UI.

Swift 5.x
Device Language we can get it using Locale.current.languageCode but it would return
en for english,
zh for Chinese-simplified , and
zh for Chinese - traditional

Actually it would more helpful when we will get something like.
zh-Hans for Chinese-simplified , and
zh-Hant for Chinese - traditional

Here zh is language code and Hans is scriptCode.

Solution:

extension Locale {
    
    static var preferredLanguageNScripCodes: [String] {
        Locale.preferredLanguages.compactMap {
            if let scriptCode = Locale(identifier: $0).scriptCode,
               let languageCode = Locale(identifier: $0).languageCode {
                return languageCode + "-" + scriptCode
            }
            return Locale(identifier: $0).languageCode
        }
    }
}

For example: ["zh-Hant", "en", "zh-Hans", "ko", "ja", "es", "de", "it", "fr", "en"]

In Swift, there is a way simpler answer to get the current app language:

Locale(identifier: Bundle.main.preferredLocalizations[0]).languageCode!

It could be switched over like this for example:

let supportUrl: URL = {
    switch Locale(identifier: Bundle.main.preferredLocalizations[0]).languageCode {
    case "de":
        return Constants.supportUrlGerman

    default:
        return Constants.supportUrlEnglish
    }
}

I hope this helps!

Related