简体   繁体   中英

In Java-8 Locale Object convert to old ISO in some Language code. How can i fix this issue without update Java version?

Like when i take input (he-IL) then output comes like "iw_IL". But i do not want this convert to old iso.

public static void main(String[] args) {
    Locale locale = getLocaleIn("he-IL");
    System.out.println(locale.toString());
}
private static Locale getLocaleIn(String langCode) {
    LocaleCode code = LocaleCode.getByCodeIgnoreCase(langCode);
    Locale locale = code.toLocale();
    return locale;
}

OutPut: iw_IL , Expected Output: he-IL

Main Problem in Here, In Java Locale.Class: 在此处输入图像描述

Finally I Want an Locale Object without Converting to old ISO.

You should avoid non-standard 3rd party classes in problem descriptions, especially when they do not contribute to the problem at all.

We can simply use

Locale locale = new Locale("he", "IL");
System.out.println(locale.toString());
locale = new Locale("iw", "IL");
System.out.println(locale.toString());

and get

iw_IL
iw_IL

under JDK 8, which is in line with the documentation :

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.

This has changed with JDK 17 :

Obsolete ISO 639 codes ("iw", "ji", and "in") are mapped to their current forms.

and running the same example program with JDK 17 accordingly now prints

he_IL
he_IL

So the cleanest and probably the only solution to your issue would be updating the Java version. Any patch forcing Locale to return the new code in older versions may cause compatibility problems with other components of the runtime relying on the old 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