android 国际化

来源:互联网 发布:荒野行动知乎 编辑:程序博客网 时间:2024/06/04 19:09

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

0 0
原创粉丝点击