Search code examples
androidlocalizationlocaleandroid-support-libraryandroid-location

How to change the library locale as well when I change the app locale in android


I created an app that utilize the google map , and I use the google play service library in android.

The problem is the library does not change the locale when I change the locale in my app. But it use the locale of the phone device.

Here is how I change the language,

1) in the extended application

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


    public static void updateLanguage(Context ctx) {
        SharedPreferences langPrefs = PreferenceManager.getDefaultSharedPreferences(ctx);
        String lang = langPrefs.getString("lang", Locale.getDefault().toString());
        updateLanguage(ctx, lang);
    }

    public static void updateLanguage(Context ctx, String lang) {
        Locale locale = new Locale(lang);
        Locale.setDefault(locale);

        Configuration cfg = new Configuration();

        switch (lang) {
        case "en":
            cfg.locale = Locale.ENGLISH;
            break;
        case "tl":
            cfg.locale = new Locale("tl", "PH");
            break;
        case "fr":
            cfg.locale = Locale.FRENCH;
            break;
        }

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

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        SharedPreferences langPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext().getApplicationContext());
        String lang = langPrefs.getString("lang", Locale.getDefault().toString());
        super.onConfigurationChanged(newConfig);
        updateLanguage(this, lang);
    }

2) in the change language page

public void selectLang(String lang, int position) {
    MyApp.updateLanguage(ctx, lang);
    refreshAfterLocaleChanged(lang ,position);
}

public void refreshAfterLocaleChanged(String lang_tmp ,int position) {
    SharedPreferences langPrefs = PreferenceManager.getDefaultSharedPreferences(ctx.getApplicationContext());
    Editor editor = langPrefs.edit();
    editor.putString("lang", lang_tmp).putString("language", lang[position]).commit();
    Intent i = new Intent(ctx, Main.class);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    startActivity(i);
}

Thanks a lot for helping


Solution

  • My code for changing languages. It works for me: change locale for all the app.

    public static void applyLanguage(Context context)
    {
        Configuration config = context.getResources().getConfiguration();
    
        if (!"".equals(languageCode) && !config.locale.getLanguage().equals(languageCode))
        {
            Locale locale = new Locale(languageCode);
            Locale.setDefault(locale);
            config.locale = locale;
            context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
        }
    }
    

    The languageCode is your lang variable