如何通过货币代码设置 iOS NSNumberFormatter 语言环境? [英] How to set iOS NSNumberFormatter locale via currency code?

查看:80
本文介绍了如何通过货币代码设置 iOS NSNumberFormatter 语言环境?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法通过货币代码而不是 iOS 中的区域设置标识符为 NSNumberFormatter 设置 NSLocale?

Is there a way to set the NSLocale for a NSNumberFormatter via the currency code instead of the locale identifier in iOS?

例如,像在语言环境设置的任何一点使用USD"而不是en_US"?

Like, for instance, to use "USD" instead of "en_US" in any point of the locale setting?

我看到有一个 NSLocaleCurrencyCode 对象,但我不确定我可以使用什么方法来设置 NSNumberFormatter's locale.

I see there's a NSLocaleCurrencyCode object, but I'm not sure what method I can use from it to set the NSNumberFormatter's locale.

我目前正在使用 Swift,如果这能让这个过程变得更容易.

I'm currently using Swift, if that makes the process any easier.

为了进一步解释我在做什么,我有每个国家的货币代码(美元、英镑、日元、人民币等)的列表.用户通过这些代码选择他使用的任何货币,然后我希望 NSNumberFormatter 自动将其语言环境设置为该货币的语言环境以自动对其进行格式化.

For further explanation of what I'm doing, I have a list of every single country's currency code (USD, GBP, JPY, CNY, etc...). The user picks whichever currency he's using via these codes, and then I want the NSNumberFormatter to automatically set its locale to the locale of that currency to automatically format it.

推荐答案

您无法根据货币代码确定特定的区域设置.

You cannot determine a specific locale from currency code.

例如,具有货币代码 USD 的区域设置数量为 27,并且带有这些 NSLocaleNSNumberFormatter 发出具有 7 个变体的格式化字符串:

For example, the number of locales which have currency code USD is 27, and NSNumberFormatter with these NSLocale emits formatted string with 7 variations:

"$123,456.78":    [en_PR, es_US, en_MH, chr_US, en_AS, en_MP, es_SV, en_VG, en_VI, es_PR, haw_US, en_IO, en_UM, en_US, en_DG, en_GU]
"US$123,456.78":  [en_PW, sn_ZW, en_ZW, en_TC, nd_ZW, en_FM]
"$123.456,78":    [es_EC]
"123 456,78 US$": [pt_TL]
"$ 123456.78":    [en_US_POSIX]
"$ 123.456,78":   [nl_BQ]
"$ 123,456.78":   [lkt_US]

如果你想做你想做的,你应该自己维护一个查找表.

If you want do what you want, you should maintain a lookup table by yourself.

let currency2locale = [
    "USD": NSLocale(localeIdentifier: "en_US"),
    "GBP": NSLocale(localeIdentifier: "en_GB"),
    "JPY": NSLocale(localeIdentifier: "ja_JP"),
    ...
]

这篇关于如何通过货币代码设置 iOS NSNumberFormatter 语言环境?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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