使用非英语语言将国家/地区代码转换为设备上的国家/地区名称 [英] Converting Country codes to country names on Device with non-english language

查看:167
本文介绍了使用非英语语言将国家/地区代码转换为设备上的国家/地区名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我正在使用的代码(取自 - 将国家/地区代码转换为国家/地区名称

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"

但是,在使用其他语言的设备上,我会得到类似的内容:
\ U05d0 \ U05e8 \ U05d2 \ U05e0 \ U05d8 \ U05d9 \ U05e0 \ U05d4

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

任何想法?

推荐答案

这是希伯来语,意思是像argntynhe或argntynhe左右。 (不幸的是我不会说任何希伯来语。只是将unicode转换为有意义的东西。)

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.

试试这个。

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

我对常量@en_US并不乐观;我把它从头顶中取出来了。只需谷歌获取适当的价值。

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.

更多文档:
https://developer.apple.com/library/mac/documentation/cocoa/reference/foundation/classes/NSLocale_Class/Reference/Reference.html

这篇关于使用非英语语言将国家/地区代码转换为设备上的国家/地区名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆