close

在Android 裡
多國語言己經做得相當完善了
而 Locale 則是一個重要的相關設定物件

 

舉例來說

//得到系統目前的使用語系
String language =Locale.getDefault().getLanguage();
//得到目前區域
String country =Locale.getDefault().getCountry();
//例如簡體中文,就會得到zh和cn

//方法二---也可以得到使用語系
Locale locale = getResources().getConfiguration().locale;
String language2 = locale.getLanguage();  

 

而問題就在於 Android 7.0 API 24 時
context.getResources().getConfiguration().locale
被 deprecated (棄用) 了
新的寫法應該如下

Locale locale;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    locale = context.getResources().getConfiguration().getLocales().get(0);
} else {
    locale = context.getResources().getConfiguration().locale;
}

 

這樣我們就可以正常使用囉!

 

 

 

 

參考資料:
https://developer.android.com/reference/java/util/Locale.html







 

 


arrow
arrow
    全站熱搜

    顏澤偉 發表在 痞客邦 留言(0) 人氣()