开发者

How to get the language used by an iPhone app

开发者 https://www.devze.com 2023-01-17 16:16 出处:网络
How can I get which language an app is running on? And I don\'t mean the preference set into settings!

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!


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.


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];


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号