Java覆盖特定语言环境的语言环境设置 [英] Java override locale setting for specific locale

查看:121
本文介绍了Java覆盖特定语言环境的语言环境设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用NumberFormat.getCurrencyInstance().format(amount)将货币从BigDecimal格式化为字符串.按预期工作,问题在于我们的主要目标是荷兰市场,而默认的荷兰格式很奇怪.

I'm using NumberFormat.getCurrencyInstance().format(amount) to format currency from a BigDecimal to a string. This works as expected the problem is that our main target is the dutch market and the default dutch formatting is strange.

让我解释一下,在格式化-125时,荷兰语为€125-"(预期为-€125").英国按预期方式提供"-125.50英镑".

Let me explain, when formatting -125 I get "€ 125-" for dutch (expected was "-€125"). Uk works as expected giving "-£125.50".

我可以检查语言环境是否为荷兰语,然后在每次我要设置小数格式时都提供一个模式.但是我更喜欢一个解决方案,它可以覆盖荷兰的格式设置.我在考虑以下内容:

I could check if the locale is Dutch and then supply a pattern each time I want to format decimals. But I would prefer a solution that overrides the dutch formatting settings. I was thinking about something like the following:

Locale nlLocale = new Locale("nl", "NL");
NumberFormat.getCurrencyInstance(new Locale("nl", "NL")).setFormatPattern("€ #");

这样,每次我在格式化时都使用荷兰语语言环境时,就得到了自定义格式.是否存在类似的解决方案?

So that each time I use the dutch locale when formatting I get my custom format. Does a similar solution exists?

推荐答案

撇开该特定格式是否正确"的问题,更改"nl"语言环境的货币实例的方法是实现和为以下配置自定义 LocaleServiceProvider 数字格式服务. (提供程序类需要子类 NumberFormatProvider ,但是超类javadoc解释了如何配置提供程序.)

Leaving aside the question of whether that particular format is "correct" or not, the way to change the currency instance for the "nl" locale is to implement and configure a custom LocaleServiceProvider for the number format service. (The provider class needs to subclass NumberFormatProvider, but the superclass javadoc explains how to configure the provider.)

对于您所关注的特定情况,提供程序需要返回非标准的NumberFormat实例,但在其他情况下(大概)委托给默认提供程序.

The provider needs to return a non-standard NumberFormat instance for the particular case you are concerned about, but (presumably) delegate to the default provider in other cases.

这篇关于Java覆盖特定语言环境的语言环境设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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