How to get Locale from its String representation in Java?

前端 未结 12 1046
慢半拍i
慢半拍i 2020-12-08 00:06

Is there a neat way of getting a Locale instance from its \"programmatic name\" as returned by Locale\'s toString() method? An obvious and ugly solution would b

12条回答
  •  一向
    一向 (楼主)
    2020-12-08 00:14

    Well, I would store instead a string concatenation of Locale.getISO3Language(), getISO3Country() and getVariant() as key, which would allow me to latter call Locale(String language, String country, String variant) constructor.

    indeed, relying of displayLanguage implies using the langage of locale to display it, which make it locale dependant, contrary to iso language code.

    As an example, en locale key would be storable as

    en_EN
    en_US
    

    and so on ...

提交回复
热议问题