What is the difference between creating a locale for en-US and en_US?

后端 未结 4 1434
暗喜
暗喜 2020-12-08 18:53

I have all my ResourceBundle values in a table and formatted as per requirement. I have to change the languages on the website based on user selection in a drop

4条回答
  •  自闭症患者
    2020-12-08 19:51

    "en-US" is an IETF language tag. While Java'a Locale class was clearly based on IETF language tags, it uses underscores in place of hyphens when separating language codes from country codes (and also variants), so calling toString() on the equivalent Locale will give you en_US.

    As of Java 7 you can use Locale.forLanguageTag(String) and toLanguageTag() to convert between language tags and Locale objects.

    When converting strings to Locale objects it's a good idea to normalize by splitting components on hyphens and underscores, lowercasing the first component (the language code) and upper-casing the second component (the country code).

提交回复
热议问题