问题
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