0

I want to change the locale within my app. I have several strings.xml file depending on various locales.Issue I am facing is the app locale gets updated but the strings like in title or tabs header doesn't gets updated depending on locale changed.

I have killed my app using System.exit(0) and manually restarted it but changes doesn't gets reflected. Please help.

Locale locale = new Locale(language);
    Locale.setDefault(locale);

    Resources res = context.getResources();
    Configuration config = new Configuration(res.getConfiguration());
    if (Build.VERSION.SDK_INT >= 17) {
        config.setLocale(locale);
        context = context.createConfigurationContext(config);
    } else {
        config.locale = locale;
        res.updateConfiguration(config, res.getDisplayMetrics());
    }

    res.updateConfiguration(config, res.getDisplayMetrics());
  • 2
    Share your code for change locale – Bhavesh Desai Feb 06 '18 at 10:23
  • Locale locale = new Locale(language); Locale.setDefault(locale); Resources res = context.getResources(); Configuration config = new Configuration(res.getConfiguration()); if (Build.VERSION.SDK_INT >= 17) { config.setLocale(locale); context = context.createConfigurationContext(config); } else { config.locale = locale; res.updateConfiguration(config, res.getDisplayMetrics()); } res.updateConfiguration(config, res.getDisplayMetrics()); – Arcgate qa007 Feb 06 '18 at 10:25
  • 1
    Share your code, by edit your question - not in a comment section. – timiTao Feb 06 '18 at 10:29
  • Arcgate qa007, try my code, i am also using in the fragment, so it might help you. – Ram Koti Feb 06 '18 at 11:13

2 Answers2

0

You can use bellow code to change locale

public void setLocale(Context context, String language)
{
    Locale newLocale;

    switch (language)
    {
        case "UK":
            newLocale = Locale.UK;
            break;

        default:
            newLocale = Locale.US;
            break;
    }

    Locale.setDefault(newLocale);
    Configuration config = new Configuration();

    if (Build.VERSION.SDK_INT >= 17) {
        config.setLocale(newLocale);
    } else {
       config.locale = newLocale;
    }

    context.getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
}

Note: You must use this code on create of MainActivity.

To restart your App you can use this:

public void resetApp(Context context) {
    Intent intent = context.getPackageManager().getLaunchIntentForPackage(getBaseContext().
    getPackageName());
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);
}

Java Supported Locales

Android - Get List of supported Locales

Fred
  • 3,365
  • 4
  • 36
  • 57
0

Write one util file for localization language :

public class LocaleUtil {

public static Configuration onAttach(Context context) {
    String lang = SessionSharedPrefs.getInstance().getLanguage();
    if (lang == null || lang.isEmpty()) {
        lang = "en";
    }
    return setLocale(context, lang);
}

public static Configuration setLocale(Context context, String language) {
    return updateResourcesLegacy(context, language);
}

@SuppressWarnings("deprecation")
private static Configuration updateResourcesLegacy(Context context, String language) {
    Locale locale = new Locale(language);
    Locale.setDefault(locale);

    Resources resources = context.getResources();

    Configuration configuration = resources.getConfiguration();
    configuration.locale = locale;

    resources.updateConfiguration(configuration, resources.getDisplayMetrics());

    return configuration;
}
}

You can use this class util like:

//language should be in language code like "en", "te";
android.content.res.Configuration configuration = LocaleUtil.setLocale(getActivity(), language);
onConfigurationChanged(configuration);
Ram Koti
  • 2,203
  • 7
  • 26
  • 36