Language is not changing in AlertDialog when changing English to Arabic

Viewed 3882

I am working on a application in which I have to change apps content from English to Arabic and vice versa. Language is being changed according to language selection in Activities and Fragments but sometimes its doesn't change in AlertDialog. Please suggest me what I am doing wrong.

Below are the details.

Class & Method to show dialog

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

    }

}
}

Method which is calling above method

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

Method being used in every activity to change content's language

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

Button click event on which language is being changed | 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();
}
2 Answers
@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);
}

By first start of app your language is default

You check always for Arabic and thats for begin is always false. By click on Button it will be set to Arabic, and by second click to Englich

onRestart() contains current selected language. change to

@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();
}
Related