---- 2017 年 3 月 28 日更新 ----
\n\n当您通过 Xcode 中的“编辑方案”设置应用程序的语言和区域时,您将获得组合的区域设置标识符es-419_MX。但是,当您通过进入设置来更改设备/模拟器的实际语言和区域时,您将获得“正确的”区域设置标识符es_MX,同时保留 的语言代码es-419,这可以有效解决几乎每个用例的问题。
// After setting language and region in Edit Scheme from Xcode\nprint(Bundle.main.preferredLocalizations) // ["es-419", "es"]\nprint(Locale.current) // es-419_MX (current)\n\n// After setting the language and region from Settings\nprint(Bundle.main.preferredLocalizations) // ["es-419", "es"]\nprint(Locale.current) // es_MX (current)\nRun Code Online (Sandbox Code Playgroud)\n\n- - /更新 - -
\n\n我正在将我的应用程序本地化为拉丁美洲西班牙语 ( es-419)。当我尝试使用 显示本地化货币时NumberFormatter,iOS 返回一个圣甲虫\xc2\xa4而不是美元符号$。
例如,如果用户的区域是墨西哥,则他们将具有区域代码es-419_MX。$以下代码没有返回,而是返回\xc2\xa4
let formatter = NumberFormatter()\nformatter.locale = Locale(identifier: "es-419_MX")\nformatter.numberStyle = .currency\nformatter.currencySymbol // \xc2\xa4\nRun Code Online (Sandbox Code Playgroud)\n\n如果删除“419”,我会得到正确的货币符号:
\n\nlet formatter = NumberFormatter()\nformatter.locale = Locale(identifier: "es_MX")\nformatter.numberStyle = .currency\nformatter.currencySymbol // $\nRun Code Online (Sandbox Code Playgroud)\n\nLocale.current返回时是否可以获得正确的货币符号es-419_MX?或者我是否必须求助于419从区域设置代码中删除 的实例?
实际代码:
\n\nfunc localizedCurrency(value: Double) -> String {\n let formatter = NumberFormatter()\n formatter.numberStyle = .currency\n formatter.locale = Locale.current\n return formatter.string(from: NSNumber(value: value)) ?? "$\\(value)"\n}\nRun Code Online (Sandbox Code Playgroud)\n
您必须进行黑客攻击,或者您必须针对每个国家/地区进行具体操作。
\n\n由于es_419它是所有拉丁美洲国家/地区的通用规范,因此它无法“猜测”您想要$显示。
语言环境“父级”下的不同国家/地区es_419具有不同的货币字符标准。例如,玻利维亚的货币符号是Bs。
ICU 区域设置 \xe2\x80\x9c 西班牙语(玻利维亚)\xe2\x80\x9d (es_BO)
\n