软件要做国际化,中英文切换少不了。这里软件只涉及中文和英文的切换。
由于后台服务器也要做国际化,所以我们app端在得到当前语言时,将当前语言存到sp文件中,请求服务器时,传递当前语言参数。
1.首先当我们安装我们软件的时候,就要根据手机当前系统语言去设置软件的语言环境。
建一个Myapplication类。在oncreate()中初始化语言。
public static String YUYAN = "";
public void onCreate() {
super.onCreate();
instance = this;
appContext = this;
//获取手机系统语言设置,如果是中文就设置成英文
//判断用户是否是第一次登陆,第一次登陆使用系统的设置。以后都用应用自己设置的语言。
//判断sp文件是否存在
String isFisrst = (String) SpUtils.get(appContext, Constants.LAUGAGE, "");
//获取系统语言
String able = getResources().getConfiguration().locale.getCountry();
//判断此时的手机系统语言跟保存的时候一致。一致的话走else,否则走if
String systemlanage = (String) SpUtils.get(appContext,Constants.SYSTEMLAUGAGE, "");
if (isFisrst.equals("") || !systemlanage.equals(able)) {
if (able.equals("CN")) {
zn(null);
SpUtils.put(appContext, Constants.LAUGAGE, "0");
YUYAN = "0";
} else {
en(null);
SpUtils.put(appContext,Constants.LAUGAGE, "1");
YUYAN = "1";
}
//第一运行时,记录下系统本地语言。
SpUtils.put(appContext, Constants.SYSTEMLAUGAGE, able);
} else {
String yueyan = (String) SpUtils.get(appContext, Constants.LAUGAGE, "");
//获取设置的语言
if (yueyan.equals("0")) {
zn(null);
YUYAN = "0";
} else if (yueyan.equals("1")) {
YUYAN = "1";
en(null);
}
}
}
en(null)和 zn(null)分别为设置app系统语言的方法。
/**
* 设置英文
*
* @param v
*/
public static void en(View v) {
Resources resources = MyApp.getContextInstance().getResources();// 获得res资源对象
Configuration config = resources.getConfiguration();// 获得设置对象
DisplayMetrics dm = resources.getDisplayMetrics();// 获得屏幕参数:主要是分辨率,像素等。
config.locale = Locale.ENGLISH; // 英文
resources.updateConfiguration(config, dm);
}
/**
* 设置中文
*
* @param v
*/
public static void zn(View v) {
Resources resources = MyApp.getContextInstance().getResources();// 获得res资源对象
Configuration config = resources.getConfiguration();// 获得设置对象
DisplayMetrics dm = resources.getDisplayMetrics();// 获得屏幕参数:主要是分辨率,像素等。
config.locale = Locale.CHINA; // 英文
resources.updateConfiguration(config, dm);
}
<application
android:name=".application.Myapplication"
android:allowBackup="true"
android:hardwareAccelerated="false"
android:icon="@mipmap/android_template"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/TranslucentTheme">
3.在app的设置选项中。实现中英文切换效果。
逻辑:1)一进入这个界面,根据sp文件中存储的中英文标识,设置当前被选中的按钮。
2)当点击中文按钮时,设置app系统语言为中文,并将中文存到sp文件中。
第一步:
@Override
public void onCreate() {
super.onCreate();
//一进入设置界面就先根据app系统语言设置默认值。
setLanguage();
}
private void setLanguage() {
//已进入到设置界面,首先要给中英文设置初始值。
//如果是0第一次进入,则根据系统语言设置当前语言
//如果不是第一次进入,则根据手动设置的语言去设置此时的界面展示。是1,选中中文。是2选中英文。
if (yueyan.equals("")) {
//获取系统语言
String able = getResources().getConfiguration().locale.getCountry();
if (able.equals("CN")) {
rg.check(R.id.cb_chinese);
} else if (able.equals("EN")) {
rg.check(R.id.cb_english);
}
} else {
if (yueyan.equals("0")) {
rg.check(R.id.cb_chinese);
} else if (yueyan.equals("1")) {
rg.check(R.id.cb_english);
}
}
}
第二步:
case R.id.cb_chinese:
//要求点击中英文,切换之后还在当前界面。
//请求跟新语言的接口
cn(v);
MyApp.YUYAN = "0";
SpUtils.put(this,Constants.LAUGAGE, "0");
final ProgressDialog pd = new ProgressDialog(this);
pd.setMessage(getString(R.string.qiehuan));
pd.show();
//请求后台服务器,成功之后调到首页。pdg.dismiss();
break;
case R.id.cb_english:
//当点击按钮时,将语言改为英文
en(v);
MyApp.YUYAN = "1";
SpUtils.put(this,Constants.LAUGAGE, "1");
final ProgressDialog pdg = new ProgressDialog(this);
pdg.setMessage(getString(R.string.qiehuan));
pdg.show();
//请求后台服务器,成功之后调到首页。pdg.dismiss();
break;