Android 獲取系統語言(兼容7.0)


轉載連接:http://likfe.com/2017/05/10/android-sys-language/

前言

獲取系統當前語言是一個比較常用的功能,在 Android 7.0 系統上舊函數獲取到的當前系統語言並不正確,或者說從 Android 7.0 起,Android 系統語言的規則變了。

下面是未適配 Android 7.0 的代碼:

//獲取 Locale 的方式有二

//方式一
Locale locale = getResources().getConfiguration().locale;

//方式二
Locale locale = Locale.getDefault();

//獲取當前系統語言
locale.getLanguage();

由於僅僅根據 getLanguage() 無法全面的了解當前的系統語言信息,比如簡體中文和繁體中文的 Language 都是 zh,所以還需要 getCountry() 方法獲取地區信息,我們就能得到 zh-CN 和 zh-HK/zh-TW 。

總結一下就是:

//獲取 Locale 的方式有二

//方式一
Locale locale = getResources().getConfiguration().locale;

//方式二
Locale locale = Locale.getDefault();

// 獲取當前系統語言
String lang = locale.getLanguage() + "-" + locale.getCountry();

但是,在 Android 7.0 上:getResources().getConfiguration().locale 被標記為 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,內容項可由用戶增刪,順序可由用戶調整。但是,此接口返回的語言順序和用戶定義的順序不一定一致!

測試語言順序
測試核心代碼:

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());
	}
}

第一次測試

測試手機:Nubia Z9 mini,Android 7.1,Mokee Rom

手機系統語言順序:hi-IN,zh-CN,en-US,zh-HK

App 國際化:values,values-zh (values 里的 string 為英文,values-zh 里的 string 為中文)

結果是:

zh-CN

0 >1> zh-CN
1 >1> hi-IN
2 >1> en-US
3 >1> zh-HK

0 >2> zh-CN
1 >2> hi-IN
2 >2> en-US
3 >2> zh-HK

0 >3> hi-IN
1 >3> zh-CN
2 >3> en-US
3 >3> zh-HK

並且 App 當前顯示的文字是中文。

第二次測試

測試手機:Nubia Z9 mini,Android 7.1,Mokee Rom

手機系統語言順序:hi-IN,en-US,zh-CN,zh-HK

App 國際化:values,values-zh

結果是:

en-US

0 >1> en-US
1 >1> hi-IN
2 >1> zh-CN
3 >1> zh-HK

0 >2> en-US
1 >2> hi-IN
2 >2> zh-CN
3 >2> zh-HK

0 >3> hi-IN
1 >3> en-US
2 >3> zh-CN
3 >3> zh-HK

並且 App 當前顯示的文字是英文。

結論

從 Android 7.0 開始,系統語言支持多個,可手動排序,系統根據 App 本身支持的語言和手機出廠設置的語言等因素來調整 App 本身的默認語言。

要獲取系統為 App 調整后的默認語言:

Locale locale = Locale.getDefault();
//Locale.getDefault() 和 LocaleList.getAdjustedDefault().get(0) 同等效果,還不需要考慮版本問題,推薦直接使用
String language = locale.getLanguage() + "-" + locale.getCountry();

要獲取系統真實首選語言:

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();


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM