我正在开发一个应用程序,我必须将应用程序内容从英语更改为阿拉伯语,反之亦然。
根据活动和碎片中的语言选择改变语言,但有时它在AlertDialog
中不会改变。请告诉我我做错了什么。
以下是详细信息。
Class&显示对话框的方法
public class AlertDialogManager {
public static void showAlertDialog(Context ctx, String message) {
if (ctx == null) {
return;
}
showAlertDialog(ctx, message, Gravity.CENTER);
}
public static void showAlertDialog(Context ctx, String message, int gravity) {
try {
AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
builder.setMessage(message).setPositiveButton("OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated
// method stub
dialog.cancel();
}
});
AlertDialog dialog = builder.show();
TextView messageText = (TextView) dialog
.findViewById(android.R.id.message);
messageText.setGravity(gravity);
} catch (Exception e) {
// TODO: handle exception
}
}
}
调用上述方法的方法
@Override
public void showAlert(String message) {
if (!TextUtils.isEmpty(message)) {
AlertDialogManager.showAlertDialog(MerchantLoginActivity.this, message);
}
}
在每项活动中使用的方法来更改内容的语言
@Override
protected void attachBaseContext(Context newBase) {
Locale languageType = LanguageUtil.getLanguageType(newBase);
super.attachBaseContext(LocaleContextWrapper.wrap(newBase, languageType));
}
正在更改语言的按钮单击事件MainActivity
@OnClick(R.id.language_button)
void onLanguageClicked(View view) {
// Getting saved language
currentLanguage = LocaleHelper.getLanguage(getBaseContext());
String languageToLoad = (currentLanguage.equals(Language.ARABIC) ? Language.ENGLISH : Language.ARABIC); // your language
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.setLocale(locale);
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
LocaleHelper.setLocale(getApplicationContext(), languageToLoad);
//LanguageUtil.changeLanguageType(getApplicationContext(), locale);
FlooosyApplication.getInstance().setLanguage(languageToLoad);
this.recreate();
//onRestart();
}
onRestart()| MainActivity
@Override
protected void onRestart() {
super.onRestart();
if (locale.equals(Language.ARABIC))
locale = Language.ENGLISH;
else
locale = Language.ARABIC;
startActivity(new Intent(this, MainActivity.class));
finish();
}
答案 0 :(得分:1)
@Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
//super.onBackPressed();
if (Locale.getDefault().getLanguage().isEmpty()) {
setLocal("en");
} else {
setLocal(Locale.getDefault().getLanguage());
}
new AlertDialog.Builder(MainActivity.this)
.setTitle(Html.fromHtml("<font color='#FF7F27'>Notic :</font>"))
.setMessage(R.string.sure)
.setCancelable(true)
.setNegativeButton(R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setPositiveButton(R.string.no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
})
.create()
.show();
}
}
private void setLocal(String lang) {
Locale locale = new Locale(lang);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
//getApplicationContext();
SharedPreferences.Editor editor = getSharedPreferences("Settings", MODE_PRIVATE).edit();
editor.putString("My_Lang", lang);
editor.apply();
}
public void loadLocale() {
SharedPreferences prefs = getSharedPreferences("Settings", Activity.MODE_PRIVATE);
String language = prefs.getString("My_Lang", "");
setLocal(language);
}
答案 1 :(得分:0)
首次启动应用时,您的语言为默认
您始终检查阿拉伯语,并且start的内容始终为 false 。 点击按钮,它将设置为阿拉伯语,然后点击 Englich
onRestart()包含当前所选语言。改为
@Override
protected void onRestart() {
super.onRestart();
if (locale.equals(Language.ARABIC))
locale = Language.ARABIC;
else
locale = Language.ENGLISH;
startActivity(new Intent(this, MainActivity.class));
finish();
}