无论国家/地区如何,以某种语言格式化货币
我想在 Java 中以用户语言格式化金额(以欧元为单位),无论用户位于哪个国家/地区:
final NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(loc);
currencyFormat.setCurrency(EUR);
currencyFormat.format(new BigDecimal("1.99"));
但是,不同输入区域设置 (loc
) 的输出如下:
- nl_NL:
1,99 欧元
- nl_BE:
1,99 欧元
- nl_DE:
1,99 欧元
- nl(
新Locale("nl")
):EUR 1,99
所以我有两个问题
- 输出确实取决于国家/地区,而不仅仅是语言(如前 2 个所示),但国家/地区应该'对我来说没关系。例如,我的游戏可以在非洲旅行时用荷兰语玩。
- 当使用没有国家/地区或国家/地区与语言不匹配的区域设置(第二个 2)时,不会打印货币符号。
请注意,我正在为游戏开发支付界面,因此 nl_DE 的区域设置意味着界面和货币应以荷兰语显示,但它仅显示德国可用的支付方式。
I want to format an amount of money (in Euros) in the user's language in Java, regardless of the country the user is in:
final NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(loc);
currencyFormat.setCurrency(EUR);
currencyFormat.format(new BigDecimal("1.99"));
However, the output for different input locales (loc
) is as follows:
- nl_NL:
€ 1,99
- nl_BE:
1,99 €
- nl_DE:
EUR 1,99
- nl (
new Locale("nl")
):EUR 1,99
So I have two problems
- The output does depend on country, not just language (as the first 2 show), but country shouldn't matter to me. My game can be played for example in Dutch while traveling in Africa.
- When using a locale without a country or with a country that doesn't match the language (second 2), no symbol for the currency is printed.
Note that I'm developing a payment interface for the game, so a locale of nl_DE means that the interface and currencies should be displayed in Dutch, but it shows only payment methods available in Germany.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不太确定你在做什么,但似乎你有一个网络应用程序,并且你需要有关语言和国家/地区的单独信息。好的,让我澄清一下:
现在,关于如何做到这一点。最简单的方法是建立某种用户配置文件,让用户决定什么是最佳的格式化区域、什么是翻译以及他想要使用什么支付系统。如果您正在开发某种基于网络的游戏,您实际上可以从 HTTP Accept-Language 标头中读取区域设置(您实际上需要设置良好的默认值,这是查找此类信息的最佳位置)。要将语言与国家/地区“分离”,您需要的只是从语言环境中读取语言和国家/地区代码,这很简单:
现在,语言通常不足以从资源包中读取文本,因为有语言环境(即 zh_CN、 zh_TW),实际语言因国家/地区而异(在上面的示例中,中国大陆和新加坡使用简体中文,而所有其他中文世界都使用繁体中文)。因此您仍然需要实际的语言环境。
国家/地区代码将为您提供所选国家/地区,但不是当前用户所在的国家/地区。这可能是好是坏,具体取决于视图。如果您需要确保付款发生在用户所在的国家/地区,恐怕您需要使用地理定位,并承担所有后果...
这是否回答了您的问题,或者我误解了什么...?
I am not exactly sure what you are up to, but it seems that you have let's say web application and you need separate pieces of information about language and country. OK, let me clarify that:
Now, on how to do that. The easiest way would be to establish some kind of user profile and let user decide on what would be the best locales for formatting, what for translation and what payment system (s)he want to use. If you are developing some kind of web-based game, you can actually read Locales from HTTP Accept-Language headers (you actually need to set good defaults and this is the best place to find such information). To "separate" language from the country, all you need is to read language and country code from locale, which is as simple as:
Now, language is often not enough to read texts from Resource Bundles, as there are Locales (i.e. zh_CN, zh_TW) for which actual language is different depending on the country (in above example Chinese Simplified is used in mainland of China as well as in Singapore, whereas all other Chinese speaking world is using Traditional Chinese). Therefore you still need actual locale.
Country code will give you the selected country, but not the one user currently is in. It might be good or bad, depending on the view. If you need to ensure that the payment happens in the country user is in, I am afraid that you would need to employ Geolocation, with all the consequences...
Does this answer your question, or I misunderstood something...?
我想我应该将付款屏幕的输入区域设置拆分为:
nl_NL
、nl_BE
、fr_FR
。用于展示东西。这是用户帐户的属性。NL
、FR
、TR
。用户确定支付方式。这是通过 IP 地址确定的,但可以手动覆盖。I'm thinking I should just split the input locale for my payment screen into:
nl_NL
,nl_BE
,fr_FR
. Used for, well, displaying stuff. This is a property of user accounts.NL
,FR
,TR
. User to determine the payment methods. This is determined via IP-address but can be manually overridden.我不清楚你所说的语言是什么意思,无论哪个国家。货币面额本身显然与语言和国家无关,但格式约定因文化而异,而不因语言而异。通常,我们将这些东西与应用程序中本地机器设置之外的其他东西联系起来,因为在不同文化中使用相同语言总是存在问题 - 例如西班牙使用 9.999,99,墨西哥使用 9,999.99,但两者(名义上)都说西班牙语。当然,仅仅因为某人生活在特定国家/地区并不意味着他们不在不同的文化环境中工作(例如,远程办公人员或跨文化边界的区域经理)。
这并不是一个简单的问题,只有通过定义期望的范围才能真正解决。
I'm not clear on what you mean by language, regardless of country. The currency denomination itself is obviously independent of language and country, but the formatting conventions vary by culture, not language. Typically we tie these things to something besides local machine settings in our applications because there are always issues of the same language being used in different cultures - for instance Spain uses 9.999,99 and Mexico uses 9,999.99 but both (nominally) speak the Spanish language. And of course, just because someone lives in a particular country does not mean they don't work in a different cultural setting (for instance, telecommuters, or regional managers who cross cultural boundaries).
It's not that simple a problem and can only really be solved by defining the scope of the expectations.
您的付款方式不应与区域设置相关联,而应与您对支付处理系统的要求相关联。然后您应该根据所选语言环境的习惯显示该值。
Your payment methods shouldn't be tied to a locale, they should be tied to what you require for the payment processing system. Then you should display that value according to the customs of the selected locale.