If mobile access is poor, please visit –> GitHub
Keywords: Android7.0
, 系统语言
,顺序不一致
Getting the current language of the system is a relatively common feature, the current system language acquired by the old function on Android 7.0 system is not correct, or from Android 7.0, the Android system language rules have changed.
Background
Below is the code that does not fit with Android 7.0:
// 获取 Locale 的方式有二Locale locale = getResources().getConfiguration().locale;Locale locale = Locale.getDefault();// 获取当前系统语言locale.getLanguage();
Because of the getLanguage()
inability to fully understand the current system language information, such as Simplified Chinese and Traditional Chinese Language are all zh
, so also need getCountry()
methods to obtain regional information, we can get zh-CN
and zh-HK/zh-TW
.
A summary is:
// 获取 Locale 的方式有二Locale locale = getResources().getConfiguration().locale;Locale locale = Locale.getDefault();// 获取当前系统语言"-" + locale.getCountry();
However, on Android 7.0: is getResources().getConfiguration().locale
marked as deprecated , so the initial adaptation is:
Locale locale;if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { locale = getResources().getConfiguration().getLocales().get(0else { locale = getResources().getConfiguration().locale;}//或者仅仅使用 locale = Locale.getDefault(); 不需要考虑接口 deprecated(弃用)问题"-" + locale.getCountry();
From Android 7.0 is used to getResources().getConfiguration().getLocales()
return an LocaleList
object, which contains >=1
a Locale, content items can be deleted by the user, the order can be adjusted by the user. However, the language order and user-defined order returned by this interface are not necessarily consistent !
Test language Order
Test the Core code:
Locale locale = Locale.getdefault (); MLOG.E (Locale.getlanguage () +"-"+ locale.getcountry ());if(Build.VERSION.SDK_INT >= build.version_codes. N) {Localelist localelist = getresources (). GetConfiguration (). Getlocales (); for(inti =0; I < localelist.size (); i++) {MLOG.E (i +">1>"+ Localelist.get (i). GetLanguage () +"-"+ Localelist.get (i). Getcountry ()); } localelist LocaleList2 = Localelist.getadjusteddefault (); for(inti =0; I < localelist2.size (); i++) {MLOG.E (i +">2>"+ Localelist2.get (i). GetLanguage () +"-"+ Localelist2.get (i). Getcountry ()); } localelist LocaleList3 = Localelist.getdefault (); for(inti =0; I < localelist3.size (); i++) {MLOG.E (i +">3>"+ Localelist3.get (i). GetLanguage () +"-"+ Localelist3.get (i). Getcountry ()); } localelist LocaleList4 = Localelist.getemptylocalelist (); for(inti =0; I < localelist4.size (); i++) {MLOG.E (i +">4>"+ Localelist4.get (i). GetLanguage () +"-"+ Localelist4.get (i). Getcountry ()); }}
First time Test
Test Phone: Nubia Z9 mini,android 7.1,mokee Rom
Phone system language Order:hi-IN,zh-CN,en-US,zh-HK
APP Internationalization: values,values-zh
(string in values is in English, string in Values-zh is Chinese)
The result is:
Zh-CN0 >1>Zh-CN1 >1>Hi-in2 >1>En-us3 >1>Zh-HK0 >2>Zh-CN1 >2>Hi-in2 >2>En-us3 >2>Zh-HK0 >3>Hi-in1 >3>Zh-CN2 >3>En-us3 >3>Zh-HK
And the text currently displayed by the APP is in Chinese .
Second Test
Test Phone: Nubia Z9 mini,android 7.1,mokee Rom
Phone system language Order:hi-IN,en-US,zh-CN,zh-HK
APP Internationalization:values,values-zh
The result is:
En-us0 >1>En-us1 >1>Hi-in2 >1>Zh-CN3 >1>Zh-HK0 >2>En-us1 >2>Hi-in2 >2>Zh-CN3 >2>Zh-HK0 >3>Hi-in1 >3>En-us2 >3>Zh-CN3 >3>Zh-HK
And the text currently displayed by the APP is in English .
Conclusion
Starting with Android 7.0, the system language supports multiple, manual sorting, and the system adjusts the default language of the app itself based on factors such as the language supported by the app itself and the language of the phone's factory settings.
To get the default language that the system adjusts for the App:
Locale locale = Locale.getDefault();//Locale.getDefault() 和 LocaleList.getAdjustedDefault().get(0) 同等效果,还不需要考虑版本问题,推荐直接使用"-" + locale.getCountry();
To get the system's true preferred language :
Locale locale;if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { locale = LocaleList.getDefault().get(0else"-" + locale.getCountry();
Reference: https://developer.android.com/reference/java/util/Locale.html
PS: You can contact me by the following way
- Weibo: cafeting
- Github:likfe
- CSDN: He called himself Mr Zhang.
Android project development Pits-get system language (compatible with 7.0)