Bruno Bieri
Bruno Bieri

Reputation: 10236

How to programmatically access the apps language in iOS 13?

Starting from iOS 13 Apple allows to:

Language selection per app

Use third‑party apps in a different language from your system language.

As I can see in the Settings app on the settings page of my app there is a new "Prefered Language" entry revealing a selection of the localized languages of my app.

Example from Facebook:

iOS App Facebook Settings

Image source


Can I programmatically access the value of the selected language?

If so with which key?

[[NSUserDefaults standardUserDefaults] objectForKey:@"key???"];

Upvotes: 2

Views: 1208

Answers (5)

Zaphod
Zaphod

Reputation: 7270

I usually add this little extension:

public extension Locale {
    /// Returns the prefered locale used in the app, if none is found, returns `Locale.current`
    static var appCurrent : Locale {
        if let prefered = Bundle.main.preferredLocalizations.first {
            return Locale(identifier: prefered)
        }
        else {
            return current
        }
    }
}

Then you can use Locale.appCurrent to retrieve it.

NOTE: I use Bundle.preferedLocalizations instead of Locale.preferedLocale because it returns the real app language. For instance, if your phone is in French and your app is only localized in English, the Bundle.preferedLocalizations will return en, because this is the Locale your app runs in. The Locale.preferedLocale will return French but all other localizations will be in English, because your app runs in English on a French device.

EDIT: Here is the Objective-C version.

@interface NSLocale(AppCurrent)

+ (NSLocale *) appCurrent;

@end

@implementation NSLocale(AppCurrent)

+ (NSLocale *) appCurrent {
    NSString * prefered = [[[NSBundle mainBundle] preferredLocalizations] firstObject];
    if (prefered != nil) {
        return [[NSLocale alloc] initWithLocaleIdentifier:prefered];
    }
    else {
        return [NSLocale currentLocale];
    }
}

@end

Then, call it this way: [NSLocale appCurrent]

Upvotes: 4

Neeraj Joshi
Neeraj Joshi

Reputation: 769

To get the Device language:

let locale = NSLocale.current.languageCode

To get the App language:

let appLang = Locale.preferredLanguages[0]

Upvotes: 0

Basir Alam
Basir Alam

Reputation: 1358

To detect the device language you can use this:

let locale = Locale.current.languageCode

Upvotes: 0

HardikS
HardikS

Reputation: 714

You may try the below code to get the application preferred language.

let appLang = Locale.preferredLanguages[0]

Upvotes: 2

Josidiah
Josidiah

Reputation: 1

I think you are trying to reach "Locale.current.languageCode"

Upvotes: 0

Related Questions