最近 Sun/Oracle 将乌克兰 hrivna 的货币代码更改为不正确的,我必须在我的代码中修复它。
我发现 Java 6 应该允许我通过 CurrencyNameProvider(另见)在 Java 级别执行此操作(详细信息在此处)。
不幸的是,我不明白哪些语言环境应该返回方法
public Locale[] getAvailableLocales() {}
欢迎任何工作示例!
// Be aware that you need to provide ukrainian names to ALL currency symbols which
// your program uses.
import java.util.Locale;
import java.util.spi.CurrencyNameProvider;
public class UaCurrencyNameProvider extends CurrencyNameProvider {
private static final Locale UKRAINE = new Locale("uk", "UA");
private static final String UAH_SYMB = "грн.";
private Locale[] availableLocales = new Locale[] {UKRAINE};
@Override
public Locale[] getAvailableLocales() {
return availableLocales;
}
@Override
public String getSymbol(String currencyCode, Locale locale) {
if(currencyCode == null) {
throw new NullPointerException();
}
else if(!UKRAINE.equals(locale)) {
throw new IllegalArgumentException("Locale not supported");
}
else if("UAH".equals(currencyCode)) {
return UAH_SYMB;
} else {
return null;
}
}
}
语言环境服务提供者:Locale sensitive factory methods and methods for name retrieval in the java.text and java.util packages invoke service provider methods when needed to support the requested locale. The methods first check whether the Java runtime environment itself supports the requested locale, and use its support if available. Otherwise, they call the getAvailableLocales() methods of installed providers for the appropriate interface to find one that supports the requested locale.
因此,如果 JRE 支持请求的区域设置,则您不能在扩展中使用自定义提供程序覆盖它,因为将首先询问默认提供程序。
示例,它尝试UAH
为 Locale xx_YY
BungaBunga和uk_UA
ungarn$$$提供一个新符号:
public class UkCurrencyNameProvider extends CurrencyNameProvider {
private static final Locale XX_YY = new Locale("xx", "YY");
private static final Locale UK_UA = new Locale("uk", "UA");
private static final Map<Locale, String> SYMBOL_MAP;
static {
SYMBOL_MAP = new HashMap<Locale, String>();
UkCurrencyNameProvider.SYMBOL_MAP.put(UkCurrencyNameProvider.XX_YY,
"BungaBunga");
UkCurrencyNameProvider.SYMBOL_MAP.put(UK_UA, "ungarn$$$");
}
private static final Locale[] AVAILABLE_LOCALES = UkCurrencyNameProvider.SYMBOL_MAP
.keySet().toArray(
new Locale[UkCurrencyNameProvider.SYMBOL_MAP.size()]);
/*
* (non-Javadoc)
* @see java.util.spi.CurrencyNameProvider#getSymbol(java.lang.String,
* java.util.Locale)
*/
@Override
public String getSymbol(final String currencyCode, final Locale locale) {
final String result;
if ("UAH".equals(currencyCode)) {
result = UkCurrencyNameProvider.SYMBOL_MAP.get(locale);
} else {
result = null;
}
return result;
}
/*
* (non-Javadoc)
* @see java.util.spi.LocaleServiceProvider#getAvailableLocales()
*/
@Override
public Locale[] getAvailableLocales() {
return UkCurrencyNameProvider.AVAILABLE_LOCALES.clone();
}
}
编码
Locale[] test=new Locale[] {new Locale("xx","YY"),new Locale("uk","UA")};
for (Locale loc:test)
{System.out.println(loc+": "+Currency.getInstance("UAH").getSymbol(loc));}
将输出
xx_YY: 邦加邦加
uk_UA: грл。
因为标准 JRE 知道 uk_UA 并将提供其已知的货币符号грл。. 对于 xx_YY,JRE 没有任何信息,并且会询问扩展中的提供者。自定义提供程序将返回BungaBunga。