Android obtains the mobile phone language environment to distinguish Simplified and Traditional, Hong Kong, Macau, and Taiwan Traditional

Both Android and IOS system languages ​​are standard: ISO 639 ISO code table

IOS: plus.os.language ios is normal, Simplified and Traditional languages ​​under Android are both zh

Android method for obtaining system language: Locale.getDefault(). language

Whether the mobile phone is switched to traditional (Taiwan, Hong Kong, Macau) or Simplified Chinese, you will get zh

At this time, I still want to distinguish Taiwan, Hong Kong, Macau or the mainland, and I need to use other methods to cooperate with it.

method 1:

First, judge according to: Locale.getDefault(). language If it is zh, you can use:

Locale.getDefault().toLanguageTag()

Data obtained by Locale.getDefault().toLanguageTag():

Hong Kong: zh-Hant-HK

Taiwan: zh-Hant-TW

Macau: zh-Hant-MO

Mainland: zh-Hans-CN

        var languages = Locale.getDefault().language
        var languageTag = Locale.getDefault().toLanguageTag()
        if (languages=="zh"){
            when(languageTag){
                "zh-Hant-TW"->{
                    //繁体 台湾
                }
                 "zh-Hant-HK"->{
                    //繁体 香港
                }
                "zh-Hant-MO"->{
                    //繁体 澳门
                }
                "zh-Hans-CN"->{
                    //简体 大陆
                }
            }
        }

Method 2:

First, judge according to: Locale.getDefault(). language If it is zh, you can use:

Locale.getDefault().country

Data obtained by Locale.getDefault(). country :

Hong Kong: HK

Taiwan: Taiwan

Macau: MO

Mainland: CN

  var languages = Locale.getDefault().language
        var country = Locale.getDefault().country
        if (languages=="zh"){
            when(country){
                "TW"->{
                    //繁体 台湾
                }
                 "HK"->{
                    //繁体 香港
                }
                "MO"->{
                    //繁体 澳门
                }
                "CN"->{
                    //简体 大陆
                }
            }
        }

--To: Daxin

Guess you like

Origin blog.csdn.net/Jason_HD/article/details/129238631