Seif E. Attia
Seif E. Attia

Reputation: 33

Translate into Arabic (Localization) issue

I'm trying to make my app support Arabic & English languages
i did it but it's not working well in some android version idk which of them...


this working well


but this not working well


I'm using the following code to in the oncreate of the mean activity of the app

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    String localLanguage = MySingleton.getmInstance(this).getAppLang();
    if (localLanguage.equals(getString(R.string.settings_language_arabic_value))) {
        Locale localeAr = new Locale("ar");
        setLocale(localeAr, getString(R.string.settings_language_arabic_value));
    } else if (localLanguage.equals(getString(R.string.settings_language_english_value))) {
        Locale localeEn = new Locale("en");
        setLocale(localeEn, getString(R.string.settings_language_english_value));
    }
    setContentView(R.layout.activity_branches);
    .......
    .......
 }

 private void setLocale(Locale locale, String lang) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        if (lang.equals(getString(R.string.settings_language_arabic_value)))
            config.setLayoutDirection(new Locale("ar"));
        else config.setLayoutDirection(new Locale("en"));
        getApplicationContext().getResources().updateConfiguration(config, null);
    } else {
        Configuration config = new Configuration();
        config.locale = locale;
        if (lang.equals(getString(R.string.settings_language_arabic_value)))
            config.setLayoutDirection(new Locale("ar"));
        else config.setLayoutDirection(new Locale("en"));
        Locale.setDefault(config.locale);
        Resources resources = getResources();
        resources.updateConfiguration(config, resources.getDisplayMetrics());
    }
}

if any one can give me a hand he will made my day
thanks in advance

Upvotes: 0

Views: 882

Answers (1)

Seif E. Attia
Seif E. Attia

Reputation: 33

finally I figured a solution ..
will share it if any one want it

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    String localLanguage = MySingleton.getmInstance(this).getAppLang();
    if (localLanguage.equals(getString(R.string.settings_language_arabic_value))) {
        setLocale("ar", this);
    } else if (localLanguage.equals(getString(R.string.settings_language_english_value))) {
        Locale localeEn = new Locale("en");
        setLocale("en", this);
    }
    setContentView(R.layout.activity_branches);
    .........
    .........
}

the change in code is here:

private void setLocale(String lang, Context context) {
    Configuration config = context.getResources().getConfiguration();
    Locale locale = new Locale(lang);
    Locale.setDefault(locale);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        config.setLocale(locale);
        getApplicationContext().getResources().updateConfiguration(config, null);
    } else {
        config.locale = locale;
        getApplicationContext().getResources().updateConfiguration(config, null);
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        config.setLocale(locale);
        config.setLayoutDirection(locale);
        context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
        context.createConfigurationContext(config);
    } else {
        context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
    }
}


I hope this help anyone

Upvotes: 1

Related Questions