3

I am trying to change the Language by pressing a button in the menubar, but i want it to switch between Norwegian and English Language. So if the the locale.toString() is "no" i'll switch to "en" and vise versa.

My problem is that i only get it to switch once and not change back if i press the button.

This is the Method used:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.Language:
            TextView textview = (TextView) findViewById(R.id.resultat);
            locale = getResources().getConfiguration().locale;
             switch(locale.toString()){
                case LOCALE_NORWEGIAN:
                    textview.append("EN ACTIVATION");
                    locale = new Locale("en");
                    break;
                case LOCALE_ENGLISH:
                    textview.append("NOR ACTIVATION");
                    locale = new Locale("nor");
                    break;
                default:
                    textview.append("DEFAULT  "+locale.toString());

            }
            Resources res = this.getResources();
            DisplayMetrics dm = res.getDisplayMetrics();
            Configuration conf = res.getConfiguration();
            conf.setLocale(locale);
            res.updateConfiguration(conf,dm);
            startActivity(new Intent(this,Game.class));
            finish();
            return true;
        case R.id.Rules:



            return true;
        default:
            return super.onOptionsItemSelected(item);
    }

}

This is how it looks, I want the Language to change when i press the button With the flagg on it. But when i press it more than once the Language won't change enter image description here

Kim Vu
  • 584
  • 9
  • 25
  • locale = new Locale("en") will only set locale for the variable and you are checking with resource default locale so, i think you need to write this line Locale.setDefault(locale); after setting value in locale – Vickyexpert Sep 12 '16 at 12:43
  • But there is a conf.setLocale(locale) where i use the locale as the argument – Kim Vu Sep 12 '16 at 12:46
  • Please provided an example use case. What are the exact steps you perform in your app and what are the results? Include screenshots to illustrate. – Code-Apprentice Sep 12 '16 at 12:47
  • can you post the declaration of LOCALE_NORWEGIAN and LOCALE_ENGLISH? new Locale("nor") isn't correct as far as i know. Java locale language and country codes are 2 chars not three. And comparing locales by using the toString isn't the best method. I would rather do something like "no".equals(locale.getLanguage()) – OH GOD SPIDERS Sep 12 '16 at 12:58
  • @911DidBush Thank you i changed the Method of comparing to locale.getLanguage() and made the LOCALE_NORWEGIAN and LOCAL_ENGLISH to "no" and "en". Now it works!! – Kim Vu Sep 12 '16 at 13:09
  • 1
    This question was answered here http://stackoverflow.com/questions/13181847/change-the-locale-at-runtime – k3b Sep 12 '16 at 13:58

2 Answers2

0

For a fully switchable activity, see this GitHub Gist. Here is a snippet:

Intent intent = getIntent();

if (locale == null) {
    Locale def = Locale.getDefault();
    Log.i(LOG_TAG + ".useLocale", "restarting the activity" +
                                  " in the default locale " + def);
    intent.putExtra(LOCALE_EXTRA, def);
} else {
    Log.i(LOG_TAG + ".useLocale", "restarting the activity in" +
                                  " the " + locale + " locale");
    intent.putExtra(LOCALE_EXTRA, locale);
}

restartingForLocaleChangeFlag = true;
overridePendingTransition(0, 0);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
overridePendingTransition(0, 0);
startActivity(intent);
tar
  • 1,538
  • 1
  • 20
  • 33
0

Seems like comparing to locale.toString() doesn't give out what i wanted, so i switched to locale.getLanguage() which gave me "en" and "no"

Kim Vu
  • 584
  • 9
  • 25