我想以编程方式更改语言。
所以我构建了两个xml文件。
values-it
-->string.xml
values-en
-->string.xml
这是MainActivity中用于更改整个应用程序语言的代码:
//意大利
Resources res = getApplicationContext().getResources();
DisplayMetrics dm = res.getDisplayMetrics();
android.content.res.Configuration conf = res.getConfiguration();
conf.locale = new Locale("it");
res.updateConfiguration(conf, dm);
// ENGLISH
Resources res2 = getApplicationContext().getResources();
DisplayMetrics dm2 = res2.getDisplayMetrics();
android.content.res.Configuration conf2 = res2.getConfiguration();
conf2.locale = new Locale("en");
res2.updateConfiguration(conf2, dm2);
现在,如果我设置英语(例如),代码执行时没有错误,但标签不会更改其文本。
如果我更改设备的方向,标签会正确更改其文本。
现在我如何修改我的代码以自动刷新标签?
答案 0 :(得分:8)
AlertDialog.Builder builder = new AlertDialog.Builder(DashboardActivity.this);
builder.setTitle(R.string.languages);
// Add the buttons
builder.setPositiveButton(R.string.english, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
String languageToLoad = "en"; // your language
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
dialog.dismiss();
rEditor.putString("language", languageToLoad);
rEditor.commit();
Intent refresh = new Intent(DashboardActivity.this, DashboardActivity.class);
startActivity(refresh);
finish();
}
});
builder.setNegativeButton(R.string.gujarati, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
String languageToLoad = "gu"; // your language
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
dialog.dismiss();
rEditor.putString("language", languageToLoad);
rEditor.commit();
Intent refresh = new Intent(DashboardActivity.this, DashboardActivity.class);
startActivity(refresh);
finish();
}
});
builder.create().show();
您必须重新加载活动以显示新语言文本意味着重新启动。
答案 1 :(得分:4)
您需要刷新活动以加载资源,而这需要更改方向。试试这个
module HackyTree exposing(..)
type HackyTree a
= Empty
|Single a
|Deep (List a) (HackyTree (List a)) (List a)
leftAdd: a -> HackyTree a -> HackyTree a
leftAdd a0 tree=
case tree of
Empty -> Single a0
Single a1 -> Deep [a0] Empty [a1]
Deep left middle right ->
case left of
[a1, a2, a3, a4] ->
Deep [a0, a1] ( leftAdd [a2, a3, a4] middle) right
_ -> Deep (a0::left) middle right
答案 2 :(得分:2)
最好的方法是将所有TextView.setText()
放在方法中。在onResume();
中调用该方法以便第一次设置它。然后在重置语言时调用该方法。 (当您更改方向时,活动将通过onStart()
onResume()
等)
答案 3 :(得分:1)
如果您还没有解决问题,可以尝试一下。它适用于我,希望它会帮助你。你可以在将来推荐它
mSwitch = (Switch) findViewById(R.id.language_switch);
mSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if (isChecked){
mSwitch.setTextColor(Color.GRAY);
String languageToLoad = "es";
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration configuration = new Configuration();
configuration.setLocale(locale);
Toast.makeText(getApplicationContext(), "spanish is set", Toast.LENGTH_SHORT).show();
getBaseContext().getResources().updateConfiguration(configuration, getBaseContext().getResources().getDisplayMetrics());
} else {
String languageToLoad = "en";
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration configuration = new Configuration();
configuration.setLocale(locale);
Toast.makeText(getApplicationContext(), "english is set", Toast.LENGTH_SHORT).show();
getBaseContext().getResources().updateConfiguration(configuration, getBaseContext().getResources().getDisplayMetrics());
}
}
});
答案 4 :(得分:0)
我们这样做:
com.android.internal.app.LocalePicker.updateLocales(LocaleList locales)
答案 5 :(得分:-4)
如何在Android App中以编程方式更改语言???
构建支持多语言的Android App示例。我是从here
找到的