Returns a locale for the specified IETF BCP 47 language tag string.
If the specified language tag contains any ill-formed subtags,
the first such subtag and all following subtags are ignored. Compare
to Locale.Builder.setLanguageTag which throws an exception
in this case.
The following conversions are performed:
Locale loc;
loc = Locale.forLanguageTag("en-US-x-lvariant-POSIX");
loc.getVariant(); // returns "POSIX"
loc.getExtension('x'); // returns null
loc = Locale.forLanguageTag("de-POSIX-x-URP-lvariant-Abc-Def");
loc.getVariant(); // returns "POSIX_Abc_Def"
loc.getExtension('x'); // returns "urp"
Locale.forLanguageTag("ar-aao").getLanguage(); // returns "aao"
Locale.forLanguageTag("en-abc-def-us").toString(); // returns "abc_US"
Locale.forLanguageTag("ja-JP-x-lvariant-JP").toLanguageTag();
// returns "ja-JP-u-ca-japanese-x-lvariant-JP"
Locale.forLanguageTag("th-TH-x-lvariant-TH").toLanguageTag();
// returns "th-TH-u-nu-thai-x-lvariant-TH"
This implements the 'Language-Tag' production of BCP47, and so supports grandfathered (regular and irregular) as well as private use language tags. Stand alone private use tags are represented as empty language and extension 'x-whatever', and grandfathered tags are converted to their canonical replacements where they exist.
Grandfathered tags with canonical replacements are as follows:
| grandfathered tag | modern replacement | |
|---|---|---|
| art-lojban | jbo | |
| i-ami | ami | |
| i-bnn | bnn | |
| i-hak | hak | |
| i-klingon | tlh | |
| i-lux | lb | |
| i-navajo | nv | |
| i-pwn | pwn | |
| i-tao | tao | |
| i-tay | tay | |
| i-tsu | tsu | |
| no-bok | nb | |
| no-nyn | nn | |
| sgn-BE-FR | sfb | |
| sgn-BE-NL | vgt | |
| sgn-CH-DE | sgg | |
| zh-guoyu | cmn | |
| zh-hakka | hak | |
| zh-min-nan | nan | |
| zh-xiang | hsn |
Grandfathered tags with no modern replacement will be converted as follows:
| grandfathered tag | converts to | |
|---|---|---|
| cel-gaulish | xtg-x-cel-gaulish | |
| en-GB-oed | en-GB-x-oed | |
| i-default | en-x-i-default | |
| i-enochian | und-x-i-enochian | |
| i-mingo | see-x-i-mingo | |
| zh-min | nan-x-zh-min |
For a list of all grandfathered tags, see the IANA Language Subtag Registry (search for "Type: grandfathered").
Note: there is no guarantee that toLanguageTag
and forLanguageTag will round-trip.
languageTag | the language tag |
NullPointerException | if languageTag is null |
toLanguageTag(), java.util.Locale.Builder.setLanguageTag(String)
Diagram: Locale