0

I tried following https://stackoverflow.com/a/42269965/1067596 and it works with two letter codes, but doesn't work with codes like b+zh+hans or pt-rBR

Looks like Locale class cannot be directly instantiated with such values as if java doesn't understand them? Can such codes be set programatically without manually parsing them?

hoshiKuzu
  • 866
  • 1
  • 12
  • 30

1 Answers1

1

For IETF BCP 47 language tags you can use the static method Locale.forLanguageTag(String). This is available since Android SDK 21. If you want to support lower SDK you can use commons-lang like described here.

In order to comply with IETF BCP 47 the tags must be separated by hyphens (-). Your first example doesn't work, seems like it should be zh-hans-.... Your second runs but seems to be wrong, too. I guess it should be either pt-BR or pt-PT.

Kindly note that the language tags are case-independent but country codes are usually written in all upper case

Locale chineseSimplifiedHongkong = Locale.forLanguageTag("zh-Hans-HK");
Locale portugueseBrazil = Locale.forLanguageTag("pt-BR");
keanni
  • 528
  • 6
  • 15