将英语更改为阿拉伯语时,AlertDialog 中的语言不会更改

Ash*_*ari 5 android android-context

我正在开发一个应用程序,在该应用程序中我必须将应用程序内容从英语更改为阿拉伯语,反之亦然。语言会根据“活动”和“片段”中的语言选择而更改,但有时在 中不会更改AlertDialog。请建议我做错了什么。

以下是详细信息。

显示对话框的类和方法

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

    }

}
}
Run Code Online (Sandbox Code Playgroud)

调用上面方法的方法

    @Override
public void showAlert(String message) {
    if (!TextUtils.isEmpty(message)) {
        AlertDialogManager.showAlertDialog(MerchantLoginActivity.this, message);
    }
}
Run Code Online (Sandbox Code Playgroud)

每个活动中使用的方法来更改内容的语言

   @Override
protected void attachBaseContext(Context newBase) {
    Locale languageType = LanguageUtil.getLanguageType(newBase);
    super.attachBaseContext(LocaleContextWrapper.wrap(newBase, languageType));
}
Run Code Online (Sandbox Code Playgroud)

正在更改语言的按钮单击事件 | 主要活动

@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();
}
Run Code Online (Sandbox Code Playgroud)

重新启动()| 主要活动

@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();
}
Run Code Online (Sandbox Code Playgroud)

ana*_*oli 0

首次启动应用程序时,您的语言为默认语言

你总是检查阿拉伯语,那就是 begin 总是false。通过单击按钮,它将设置为阿拉伯语,第二次单击将设置为英语

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();
}
Run Code Online (Sandbox Code Playgroud)