1

I'm using this application class to set my locale all over my application

public class MyApplication extends Application {
public static void updateLanguage(Context ctx) {
    SharedPreferences prefs = getDefaultSharedPreferences(ctx);
    String lang = prefs.getString("locale_override", "");
    updateLanguage(ctx, lang);

}

public static void updatefont(Context ctx) {
    SharedPreferences prefs = getDefaultSharedPreferences(ctx);

    String lang = prefs.getString("locale_override", "");
    if (lang.equals("ar")) {
        CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
                .setDefaultFontPath("fonts/Cairo-Regular.ttf")
                .setFontAttrId(R.attr.fontPath)
                .build()
        );

    } else {
    }


}

public static void updateLanguage(Context ctx, String lang) {
    Configuration cfg = new Configuration();
    if (!TextUtils.isEmpty(lang))
        cfg.locale = new Locale(lang);
    else
        cfg.locale = Locale.getDefault();

    ctx.getResources().updateConfiguration(cfg, null);
}

@Override
public void onCreate() {
    updateLanguage(this);
    updatefont(this);
    super.onCreate();


}
}

it work's fine but when I test it on Nexus 5X API 27 emulator device it doesn't work at all . all solutions not working

George
  • 6,886
  • 3
  • 44
  • 56
radwa ali
  • 11
  • 2
  • 1
    weird way what ? what is your actual problem ? – Ali Mar 05 '18 at 11:37
  • the app doesn't take the arabic orientation or the arabic resources files and strings – radwa ali Mar 05 '18 at 11:40
  • what is the **arabic orientation** and **arabic resources** – Ali Mar 05 '18 at 11:41
  • arabic strings file doesn't apply to the app – radwa ali Mar 05 '18 at 11:42
  • Possible duplicate of [Change the locale at runtime?](https://stackoverflow.com/questions/13181847/change-the-locale-at-runtime) – k3b Mar 05 '18 at 13:08
  • @k3b It's not duplicate with your one, it's duplicate with [this](https://stackoverflow.com/questions/46531579/the-app-doesnt-get-the-localization-change-effect-on-nougat-api-7) one. –  Mar 05 '18 at 13:28

1 Answers1

0

By take considering to that way you set the font to the app (not a good way) you should do this instead.

Also you should aware from using a deprecated methods to change localization, also you need to set DisplayMetrics like: DisplayMetrics dm = res.getDisplayMetrics();

Once you set language, it's better to restart application for user (better experience).

The whole method should be like:

 @SuppressWarnings("deprecation")
public static void updateLanguage(Context ctx, String lang) {
Locale locale = new Locale(lang);
Resources resources = ctx.getResources();
Configuration configuration = resources.getConfiguration();
DisplayMetrics displayMetrics = resources.getDisplayMetrics();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){
    configuration.setLocale(locale);
} else{
    configuration.locale=locale;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
    getApplicationContext().createConfigurationContext(configuration);
} else {
    resources.updateConfiguration(configuration,displayMetrics);
}


    Intent i = getBaseContext().getPackageManager()
            .getLaunchIntentForPackage(getBaseContext().getPackageName());
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    startActivity(i);
    this.finish();

}
  • @radwaali you should describe, "what's not working exactly"? - I updated my answer too. –  Mar 05 '18 at 12:06
  • @radwaali I don't know what you exactly facing, but above code works with me on as well. –  Mar 05 '18 at 12:42
  • can you provide the whole application class that work's with you – radwa ali Mar 05 '18 at 12:48
  • @radwaali there is no difference, but here is a [sample](https://github.com/ibrahimAlii/LangaugeIssue/blob/master/app/src/main/java/c/ib/langaugeissue/MainActivity.java) –  Mar 05 '18 at 12:52
  • @radwaali Can you provide which context you passed? `ctx`. –  Mar 05 '18 at 12:57
  • your example not working also when I clicked arabic button also change the direction for the app but still show the English strings – radwa ali Mar 05 '18 at 13:04
  • @radwaali ok then try to use `MainActivity.this` instead of `getApplicationContext()` in my [exampe](https://github.com/ibrahimAlii/LangaugeIssue/blob/cabeea82276464424b06c8abfd45635c2ccea7cb/app/src/main/java/c/ib/langaugeissue/MainActivity.java#L36) and it will work. –  Mar 05 '18 at 13:15