Andrea
Andrea

Reputation: 658

How to get the language used by an iPhone app

How can I get which language an app is running on? And I don't mean the preference set into settings!

Let me explain it further... I localised my app with three languages: English, Italian and Spanish. If the iPhone is set on Italian or Spanish, the app will use those two... it falls back to English otherwise.

For example: a French user gets the English version... so even if French is the language set, my app automatically use English. Now... how can I return this "value" in my code?

Thanks!

Upvotes: 2

Views: 1673

Answers (3)

Fernando Pardo
Fernando Pardo

Reputation: 91

You just need to get the current language of the device (as explained above by other users) and compare it with the languages supported by your app, something like:

currentLangue = //get current language with code in other answers
languageToUse = ""
for each language in languagesSupportedbyMyApp
  if currentLanguage == language
     languagetoUse = language

//if no language is found then set it to "en"
if languageToUse == ""
  languageToUse = "en"

regards, Fernando

Upvotes: 0

Nick Lockwood
Nick Lockwood

Reputation: 40995

You can get the user's preferred language using:

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

The [NSLocale preferredLanguages] array actually contains all the user's preferred languages in descending order, so you can just loop through it until you find one that your app supports.

Upvotes: 2

Aaron Saunders
Aaron Saunders

Reputation: 33345

Dont really undertstand what this And I don't mean the preference set into settings! means?

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSArray *languages = [defaults objectForKey:@"AppleLanguages"];
NSString *currentLanguage = [languages objectAtIndex:0];

Upvotes: 0

Related Questions