How do I get current application locale?
Asked Answered
L

6

15

I need to get current locale. Not user locale but my application locale.

Let's say my application has two localizations (in project settings): English (default) and French. If user sets French language on iPhone then my application will display French interface. If user sets German language on iPhone then my application will display English interface (because English is default).

So how do I get current application locale that is showing right now? Thanks in advance.

Latonia answered 1/5, 2015 at 12:39 Comment(0)
G
23

The selected answer returns the current device language, but not the actual language used in the app. If you don't provide a localization for the preferred language in your app:

NSString *language = NSBundle.mainBundle.preferredLocalizations.firstObject;

NSLocale *locale = NSLocale.currentLocale;
NSString *countryCode = [locale objectForKey:NSLocaleCountryCode];

NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
NSString *country = [usLocale displayNameForKey:NSLocaleCountryCode 
                                          value:countryCode];

NSLog(@"country: %@", country);
Geology answered 1/5, 2015 at 13:2 Comment(2)
Great! Is there a way i can convert it to NSLocale?Latonia
Stealing other people's answer is not something we do on Stack Overflow, especially without credit. If an answer to another question can solve it then vote to close as duplicate. Don't steal the answer.Bram
I
30

There may be an easier way than this (see other answers), but this one is most robust, and as long as the general principle of localizing an app through a strings-file doesn't get obsoleted, this method will work.

Generally, you don't need to get the application locale (but read on, it's possible!) If you want localized text, you use NSLocalizedString(). If you need to localize images you use localized resources, and so on. However, there are reasons that I can think of which would make it nice to get the "application locale", as you call it: for example for analytics (you want to know in which language your app is used), or for providing a consistent one-language interface to the user if you use server-based communication (e.g. to localize the server error messages in the same language that the user is seeing inside the app.)

If you want to get the localization of the app that is currently visible, I suppose you have a Localizable.strings file for each supported locale. So, in the Englisch strings-file you can add the line

"lang" = "en";

and in the French string-file you add the line

"lang" = "fr";

then, you can always get the application-locale by calling NSLocalizedString("lang") (swift) or NSLocalizedString(@"lang") (objective-C). And of course, whenever you add a new localization to your app, you have to set a "lang" entry into the new localizations strings-file.

Indorse answered 1/5, 2015 at 13:9 Comment(0)
G
23

The selected answer returns the current device language, but not the actual language used in the app. If you don't provide a localization for the preferred language in your app:

NSString *language = NSBundle.mainBundle.preferredLocalizations.firstObject;

NSLocale *locale = NSLocale.currentLocale;
NSString *countryCode = [locale objectForKey:NSLocaleCountryCode];

NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
NSString *country = [usLocale displayNameForKey:NSLocaleCountryCode 
                                          value:countryCode];

NSLog(@"country: %@", country);
Geology answered 1/5, 2015 at 13:2 Comment(2)
Great! Is there a way i can convert it to NSLocale?Latonia
Stealing other people's answer is not something we do on Stack Overflow, especially without credit. If an answer to another question can solve it then vote to close as duplicate. Don't steal the answer.Bram
E
14

In Swift 3 use:

let language = Bundle.main.preferredLocalizations.first

Elbe answered 1/5, 2015 at 12:56 Comment(0)
W
4

You can get country code from locale, this way,

NSLocale *currentLocale = [NSLocale currentLocale];  // get the current locale.
NSString *countryCode = [currentLocale objectForKey:NSLocaleCountryCode];

Get country code, e.g. es (Spain), fr (France), etc.

You can map this code for your interface selection.

Hope this help you.

Wane answered 1/5, 2015 at 12:42 Comment(0)
V
1

you can get selected language using following code.

NSString * language = [[NSLocale preferredLanguages] objectAtIndex:0];

if ([language isEqualToString:@"fr"]){//French
    [do  your stuff]
}
else{//English

   [do  your stuff]
}

List_of_ISO_639-1_codes

Hope this will help you.

Vierno answered 1/5, 2015 at 13:5 Comment(1)
As mentioned, this gives you he iPhone System language. [[NSBundle mainBundle] preferredLocalizations].firstObject gives the "best match" of the apps languages with the users prioritised languages list.Minier
S
0

The solution is way simpler than the ones proposed:

Device language

let deviceLanguage = NSLocale.preferredLanguages.first

Actual app translation

let language = Bundle.main.preferredLocalizations.first

For instance, I set my iPhone in English-Indian and my app doesn't have English-Indian translation but only English-UK:

Printing description of language:
"en-GB"
Printing description of deviceLanguage:
"en-IN"
Sedge answered 10/1, 2020 at 14:22 Comment(1)
For some reason Locale.preferredLanguages[0] gives me just en. I don't get the country code.Lining

© 2022 - 2024 — McMap. All rights reserved.