Beispiel
Kategorie
Globalisierung : Handhabung von Locales

Name
Verwenden Sie Locale-Namen nicht als Zeichenfolgeliterale

Erläuterung
Die Namen von Locales in Java sind nicht festgelegt und können später geändert werden. Jede Locale wird allerdings durch ein eindeutiges Objekt Locale dargestellt, das abwärts kompatibel ist. Verwenden Sie das Objekt Locale anstatt der Zeichenfolge mit dem Locale-Namen.

Lösung
Verwenden Sie das Objekt Locale als Parameter in Methoden, die Locale-abhängig sind.

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 );
}
}