1

I try to use two languages in my application.I have different string.xml files in my project. I try to change language like this

public class LocaleManager {

public static final String LANGUAGE_ENGLISH   = "en";
public static final String LANGUAGE_GEORGIAN = "ka";
private static final String LANGUAGE_KEY       = "language_key";

public static Context setLocale(Context c) {
    return updateResources(c, getLanguage(c));
}

public static Context setNewLocale(Context c, String language) {
    persistLanguage(c, language);
    return updateResources(c, language);
}

public static String getLanguage(Context c) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c);
    return prefs.getString(LANGUAGE_KEY, LANGUAGE_ENGLISH);
}

@SuppressLint("ApplySharedPref")
private static void persistLanguage(Context c, String language) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c);

    prefs.edit().putString(LANGUAGE_KEY, language).commit();
}

private static Context updateResources(Context context, String language) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        Resources resources = context.getResources();
        Configuration configuration = resources.getConfiguration();
        configuration.setLocale(new Locale(language));
        context.getApplicationContext().createConfigurationContext(configuration);
    } else {
        Locale locale = new Locale(language);
        Locale.setDefault(locale);
        Configuration config = context.getResources().getConfiguration();
        config.locale = locale;
        context.getResources().updateConfiguration(config,
                context.getResources().getDisplayMetrics());
    }
    return context;
}

public static Locale getLocale(Resources res) {
    Configuration config = res.getConfiguration();
    return Build.VERSION.SDK_INT >= 24 ? config.getLocales().get(0) : config.locale;
}

I'm using my class like this in button onClick

 flagImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!LocaleManager.getLanguage(LauncherActivity.this).equals(LocaleManager.LANGUAGE_ENGLISH))
                    LocaleManager.setNewLocale(LauncherActivity.this, LocaleManager.LANGUAGE_ENGLISH);
                else
                    LocaleManager.setNewLocale(LauncherActivity.this, LocaleManager.LANGUAGE_GEORGIAN);


                Intent i = new Intent(LauncherActivity.this, LauncherActivity.class);
                startActivity(i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK));
                overridePendingTransition(R.anim.fade_in_dialog, R.anim.fade_out_dialog);

            }
        });

I have a two questions:

1) I tested it and locale is changing successfully but I can't change setText. Is it a possible to change setText without refresh activity?

2) After locale has changed I try to use my setText in onCreate method, but still can't change it

How I can solve this problem?

Brett DeWoody
  • 59,771
  • 29
  • 135
  • 184
BekaKK
  • 2,173
  • 6
  • 42
  • 80

0 Answers0