3

Why Java is showing ISO 639-2 language codes incorrectly in case of Finnish language? With ISO 639 codes it gives the correct language name in Finnish.

public static void main(String[] args) {
    Locale fi = new Locale("fi");
    Locale fin = new Locale("fin");
    Locale en = new Locale("en", "EN");
    Locale sv = new Locale("sv");

    System.out.println("fi: " +fi.getDisplayLanguage(fi));
    System.out.println("fin: " +fin.getDisplayLanguage(fi));
    System.out.println("sv: " +fin.getDisplayLanguage(sv));
    System.out.println("en: " +fin.getDisplayLanguage(en));


}

fi: suomi
fin: Finnish***?? Why this is in English, not in Finnish?
sv: Finska
en: Finnish
Mar Foo
  • 33
  • 7

1 Answers1

4

This is because "fin" is not a valid value for the language argument of the Locale(String) constructor.

According to the documentation, the language argument should be (emphasis mine):

ISO 639 alpha-2 or alpha-3 language code, or registered language subtags up to 8 alpha letters (for future enhancements). When a language has both an alpha-2 code and an alpha-3 code, the alpha-2 code must be used. You can find a full list of valid language codes in the IANA Language Subtag Registry (search for "Type: language"). The language field is case insensitive, but Locale always canonicalizes to lower case.

Because Finnish has an alpha-2 code ("fi"), this is what you should use, and not "fin". Otherwise you are effectively creating a Locale object for some sort of default Locale (English?).

Grodriguez
  • 21,501
  • 10
  • 63
  • 107
  • Weird that it shows correctly in Swedish though. Then again, `Locale` has plenty of problems. – Kayaman Jan 13 '16 at 15:15
  • Not sure about that. I tested it myself and it shows:`fi: suomi, fin: fin, sv: fin, en: fin`, which makes sense to me. Perhaps the code snippet posted by the OP does not exactly correspond to the output shown in the question? – Grodriguez Jan 13 '16 at 15:16
  • I get the same output as OP. Disclaimer: I'm Finnish. – Kayaman Jan 13 '16 at 15:19
  • Uhm, then I am not sure what is going on internally, but in any case the Javadocs clearly state that an alpha-2 code should be used if both alpha-2 and alpha-3 codes exist. – Grodriguez Jan 13 '16 at 15:21
  • Well, I assume we have different locale information in our Java installations. But your answer still stands, especially in light of this additional discrepancy. – Kayaman Jan 13 '16 at 15:26
  • But fin is ISO 639-2 code, not fi. Fi is ISO 639-1. – Mar Foo Jan 13 '16 at 20:07
  • @MarFoo That is exactly the issue. The Javadocs say that if there is an alpha-2 code (ISO 639-1) and an alpha-3 code (ISO 639-2), then the alpha-2 code must be used. – Grodriguez Jan 14 '16 at 08:08
  • @Grodriguez So ISO 639 alpha-2 means iso639-1? OK, so then the documentation is correct, still I wonder that why Swedish and English is working, but not Finnish in case three letters codes. fin.getDisplayLanguage(fi)) is not working, but fin.getDisplayLanguage(sv) is. – Mar Foo Jan 14 '16 at 08:44
  • @MarFoo Yes -- the name is a bit misleading but iso639-1 is the one that actually defines the alpha-2 space (see for example: https://en.wikipedia.org/wiki/ISO_639) – Grodriguez Jan 14 '16 at 08:51