How do I get current application locale?

Alexander picture Alexander · May 1, 2015 · Viewed 27.9k times · Source

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.

Answer

Michael picture Michael · May 1, 2015

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.