Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get current application locale?

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.

like image 726
Alexander Avatar asked May 01 '15 12:05

Alexander


People also ask

How can I get current locale language in Android?

You can use Locale. getDefault(). getLanguage(); to get the usual language code (e.g. "de", "en").

How do I find my locale in Java?

To get equivalent information in Java, use Locale. getDefault() to get the Locale that Java is using, and use methods on the Locale object such as getCountry() , getLanguage() to get details. The information is available using ISO codes and as human readable/displayable names.

What is use locale default?

The default locale is appropriate for tasks that involve presenting data to the user. In this case, you want to use the user's date/time formats, number formats, rules for conversion to lowercase, and so on. In this case, it's safe to use the convenience methods.


2 Answers

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.

like image 161
Michael Avatar answered Oct 06 '22 10:10

Michael


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);
like image 33
Vijay yadav Avatar answered Oct 06 '22 09:10

Vijay yadav