天天看點

android 國際化

1、在res的檔案夾下面建立你要國際化語言的檔案夾values

2、在每個values的檔案夾中的string.xml檔案中建立相同名稱的名值對。

3、就是在每個界面中設定你選擇的語言的類型、設定完成後再為控件設定文字setText()

Configuration config = getResources().getConfiguration();

Locale systemLacale = BaseApp.getSystemLacale(this);

config.setLocale(systemLacale);

getResources().updateConfiguration(config,getResources().getDisplayMetrics());

4、同時也要在onResume()中重新設定控件的文字,這樣是為了可以在使用者選擇語言後可以重新整理界面

5、在使用者選擇了語言的類型後,将選擇語言類型、國家儲存在本地。

public static void setSystemLacale(Context context, Locale locale) {

SharedPreferences sharedPreferences = SharedPreferencesUtils

.getCurrentSharedPreferences(context);

SharedPreferences.Editor editor = sharedPreferences.edit();

editor.putString(SYSTEM_LOCALE_LANGUAGUE_STRING, locale.getLanguage());

editor.putString(SYSTEM_LOCALE_COUNTRY_STRING, locale.getCountry());

editor.commit();

}

6、當使用者關閉應用程式重新進入的時候應該加載的是使用者選擇的語言類型

public static Locale getSystemLacale(Context context) {

SharedPreferences sharedPreferences = SharedPreferencesUtils

.getCurrentSharedPreferences(context);

String str = sharedPreferences.getString(

SYSTEM_LOCALE_LANGUAGUE_STRING, no_languague);// 這個是擷取語言類型

String strc = sharedPreferences.getString(SYSTEM_LOCALE_COUNTRY_STRING,

null);// 這個是擷取語言的國家

if (null == str) {

Locale l = Locale.getDefault();// 這個是跟随系統的設定

return l;

}

if (no_languague.equals(str)) {

Locale l = Locale.getDefault();// 這個是跟随系統的設定

String def = "en";

for (int i = 0; i < ENAME.length; i++) {

if (ENAME[i].equals(no_languague)) {

def = ENAME[i];

break;

}

}

Locale nLocale = null;

if ("zh".equals(def)) {

if ("CN".equals(l.getCountry())) {

nLocale = Locale.SIMPLIFIED_CHINESE;

} else {

nLocale = Locale.TRADITIONAL_CHINESE;

}

} else {

nLocale = new Locale(def);

}

// setSystemLacate(context, nLocale);

return nLocale;

}

return new Locale(str, strc);

}

最後附上源碼http://download.csdn.net/detail/qq_24391625/9650781