Search code examples
javaruntimelocale

setting language in a Locale


I would like to know why the following compiles:

Locale aLocale = new Locale.Builder().setLanguage("trollolo").setRegion("CA").build();
System.out.println(aLocale.getLanguage());

Since "trollolo" is not a language. Why does it not return a run time exception but instead it "trollolo" gets even returned when called getLanguage()?


Solution

  • This is permitted behavior. From the javadoc

    The Builder only checks if an individual field satisfies the syntactic requirement (is well-formed), but does not validate the value itself.

    The syntax (IETF BCP 47) is defined in Tags for Identifying Languages