前言
獲取系統(tǒng)當(dāng)前語言是一個比較常用的功能,在 Android 7.0 系統(tǒng)上舊函數(shù)獲取到的當(dāng)前系統(tǒng)語言并不正確,或者說從 Android 7.0 起,Android 系統(tǒng)語言的規(guī)則變了。
下面是未適配 Android 7.0 的代碼:
//獲取 Locale 的方式有二//方式一Locale locale = getResources().getConfiguration().locale;//方式二Locale locale = Locale.getDefault();//獲取當(dāng)前系統(tǒng)語言locale.getLanguage();
由于僅僅根據(jù) getLanguage() 無法全面的了解當(dāng)前的系統(tǒng)語言信息,比如簡體中文和繁體中文的 Language 都是 zh,所以還需要 getCountry() 方法獲取地區(qū)信息,我們就能得到 zh-CN 和 zh-HK/zh-TW 。
總結(jié)一下就是:
//獲取 Locale 的方式有二//方式一Locale locale = getResources().getConfiguration().locale;//方式二Locale locale = Locale.getDefault();// 獲取當(dāng)前系統(tǒng)語言String lang = locale.getLanguage() + "-" + locale.getCountry();
但是,在 Android 7.0 上:getResources().getConfiguration().locale 被標(biāo)記為 deprecated 了,所以初步適配后是:
//獲取 Locale 對象的正確姿勢:Locale locale;if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { locale = getResources().getConfiguration().getLocales().get(0);} else { locale = getResources().getConfiguration().locale;}//獲取語言的正確姿勢:String lang = locale.getLanguage() + "-" + locale.getCountry();從 Android 7.0 起使用的getResources().getConfiguration().getLocales() 返回的是一個 LocaleList 對象,它包含 >=1 個 Locale,內(nèi)容項可由用戶增刪,順序可由用戶調(diào)整。但是,此接口返回的語言順序和用戶定義的順序不一定一致!
測試語言順序
測試核心代碼:
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 (int i = 0; i < localeList.size(); i++) { MLog.e(i + " >1> " + localeList.get(i).getLanguage() + "-" + localeList.get(i).getCountry()); } LocaleList localeList2 = LocaleList.getAdjustedDefault(); for (int i = 0; i < localeList2.size(); i++) { MLog.e(i + " >2> " + localeList2.get(i).getLanguage() + "-" + localeList2.get(i).getCountry()); } LocaleList localeList3 = LocaleList.getDefault(); for (int i = 0; i < localeList3.size(); i++) { MLog.e(i + " >3> " + localeList3.get(i).getLanguage() + "-" + localeList3.get(i).getCountry()); } LocaleList localeList4 = LocaleList.getEmptyLocaleList(); for (int i = 0; i < localeList4.size(); i++) { MLog.e(i + " >4> " + localeList4.get(i).getLanguage() + "-" + localeList4.get(i).getCountry()); }}第一次測試
測試手機(jī):Nubia Z9 mini,Android 7.1,Mokee Rom
手機(jī)系統(tǒng)語言順序:hi-IN,zh-CN,en-US,zh-HK
App 國際化:values,values-zh (values 里的 string 為英文,values-zh 里的 string 為中文)
結(jié)果是:
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
并且 App 當(dāng)前顯示的文字是中文。
第二次測試
測試手機(jī):Nubia Z9 mini,Android 7.1,Mokee Rom
手機(jī)系統(tǒng)語言順序:hi-IN,en-US,zh-CN,zh-HK
App 國際化:values,values-zh
結(jié)果是:
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
并且 App 當(dāng)前顯示的文字是英文。
結(jié)論
從 Android 7.0 開始,系統(tǒng)語言支持多個,可手動排序,系統(tǒng)根據(jù) App 本身支持的語言和手機(jī)出廠設(shè)置的語言等因素來調(diào)整 App 本身的默認(rèn)語言。
要獲取系統(tǒng)為 App 調(diào)整后的默認(rèn)語言:
Locale locale = Locale.getDefault();//Locale.getDefault() 和 LocaleList.getAdjustedDefault().get(0) 同等效果,還不需要考慮版本問題,推薦直接使用String language = locale.getLanguage() + "-" + locale.getCountry();
要獲取系統(tǒng)真實首選語言:
Locale locale;if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { locale = LocaleList.getDefault().get(0);} else { locale = Locale.getDefault();}String language = locale.getLanguage() + "-" + locale.getCountry();以上這篇Android 獲取系統(tǒng)語言的實例(兼容7.0)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持VEVB武林網(wǎng)。
新聞熱點
疑難解答
圖片精選