示例
类别
全球化: 语言环境处理

名称
避免将语言环境名称用作字符串文字

说明
Java 中的语言环境名称并不是固定的,将来有可能会更改。但是,每种语言环境是用向后兼容的唯一 Locale 对象表示的。使用 Locale 对象来代替带有语言环境名称的字符串。

解决方案
Locale 对象用作区分语言环境的方法中的参数。

public static final String CHINESE_LOCALE_STRING = "CHINESE"; //$NON-NLS-1$

private static void processEnglishString(String str) {
// process string in US locale
}

private static void processChineseString(String str) {
// process string in CHINESE locale
}
public static void processString(String str, Locale locale){
if (Locale.CHINA.equals(locale)){
processChineseString(str);
}else {
processEnglishString(str);
}
}


public static void main(String[] args) {
String str = "\u4e1c\u897f\u5357\u5317\u4e2d";//$NON-NLS-1$
if ( CHINESE_LOCALE_STRING.equals( args[0] ) ){
processString( str, Locale.CHINA );
}else {
processString( str, Locale.US );
}
}