简体   繁体   中英

JDK Locale class handling of ISO language codes for Hebrew (he), Yiddish (yi) and Indonesian (id)

When instantiating a Locale<\/code><\/a> object with either one of the following language codes: he<\/code> , yi<\/code> and id<\/code> it doesn't preserve their value.

Locale locale = new Locale("he", "il");
locale.getLanguage(); // -> "iw"

The Locale class does not impose any checks on what you feed in it, but it swaps out certain language codes for their old values. From the documentation :

ISO 639 is not a stable standard; some of the language codes it defines (specifically "iw", "ji", and "in") have changed. This constructor accepts both the old codes ("iw", "ji", and "in") and the new codes ("he", "yi", and "id"), but all other API on Locale will return only the OLD codes.

Here's the constructor:

public Locale(String language, String country, String variant) {
    this.language = convertOldISOCodes(language);
    this.country = toUpperCase(country).intern();
    this.variant = variant.intern();
}

And here's the magic method:

private String convertOldISOCodes(String language) { 
    // we accept both the old and the new ISO codes for the languages whose ISO 
    // codes have changed, but we always store the OLD code, for backward compatibility 
    language = toLowerCase(language).intern(); 
    if (language == "he") { 
        return "iw"; 
    } else if (language == "yi") { 
        return "ji"; 
    } else if (language == "id") { 
        return "in"; 
    } else { 
        return language; 
    }
}

The objects it creates are immutable, so there's no working around this. The class is also final , so you can't extend it and it has no specific interface to implement. One way to make it preserve those language codes would be to create a wrapper around this class and use that.

The Java treatment of the Hebrew locale seems to had been changed in Java 17<\/strong> . It appears as an attempt to adhere to the ISO_639-1 language codes standard.

This means you will succeed to load a Hebrew resource bundle named ' messages_he.properties<\/em> ' with either 'iw' or 'he' language code constructed locales. A ' messages_iw.properties<\/em> ' resource is de-prioritized and will only get loaded if a corresponding 'he' resource is none existent.

Just use the 'he' ISO code.

. I've provided a small example class with basic resource bundles which demonstrates the new behavior.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM