Converting Country codes to country names on Device with non-english language

人走茶凉 提交于 2019-12-02 07:04:36

问题


This is the code I am using (Taken from - Converting Country Codes to Country Names)

  NSLocale *locale = [NSLocale currentLocale];
  NSString *countryCode = [locale objectForKey: NSLocaleCountryCode];
  NSString *identifier = [NSLocale localeIdentifierFromComponents: [NSDictionary dictionaryWithObject: countryCode forKey: NSLocaleCountryCode]];
  NSString *country = [[NSLocale currentLocale] displayNameForKey: NSLocaleIdentifier value: identifier];

On device with english I'll get this: "Argentina"

But, on device with other language i'll get something like: "\U05d0\U05e8\U05d2\U05e0\U05d8\U05d9\U05e0\U05d4"

Any idea?


回答1:


It is Hebrew and means something like argntynhe or argntynhe or so. (Unfortunately I do not speak any Hebrew. Just converted the unicode to something meaningful.)

Check the language settings of your device or the locale identifier that you used to initialize your locale with.

Try this.

NSLocale myLocale = [NSLocale localeWithLocaleIdentifier:@"en_US"];
NSString *country = [myLocale displayNameForKey: NSLocaleIdentifier value: identifier];

I am just not positive about the constant @"en_US"; I took that out of the top of my head. Just google for an appropriate value.

Further docs: https://developer.apple.com/library/mac/documentation/cocoa/reference/foundation/classes/NSLocale_Class/Reference/Reference.html



来源:https://stackoverflow.com/questions/22986049/converting-country-codes-to-country-names-on-device-with-non-english-language

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!