Reputation: 10236
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:
Can I programmatically access the value of the selected language?
If so with which key?
[[NSUserDefaults standardUserDefaults] objectForKey:@"key???"];
Upvotes: 2
Views: 1208
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 ofLocale.preferedLocale
because it returns the real app language. For instance, if your phone is in French and your app is only localized in English, theBundle.preferedLocalizations
will returnen
, because this is theLocale
your app runs in. TheLocale.preferedLocale
will returnFrench
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
Reputation: 769
To get the Device language:
let locale = NSLocale.current.languageCode
To get the App language:
let appLang = Locale.preferredLanguages[0]
Upvotes: 0
Reputation: 1358
To detect the device language you can use this:
let locale = Locale.current.languageCode
Upvotes: 0
Reputation: 714
You may try the below code to get the application preferred language.
let appLang = Locale.preferredLanguages[0]
Upvotes: 2