4

Following is the Application code of my Android app:

public class MyApplication extends Application
{
    private Locale locale = null;

    @Override
    public void onConfigurationChanged(Configuration newConfig)
    {
        super.onConfigurationChanged(newConfig);
        if (locale != null)
        {
            newConfig.locale = locale;
            Locale.setDefault(locale);
            getBaseContext().getResources().updateConfiguration(newConfig, getBaseContext().getResources().getDisplayMetrics());
        }
    }

    @Override
    public void onCreate()
    {
        super.onCreate();
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
        Configuration config = getBaseContext().getResources().getConfiguration();

        String lang = settings.getString(getString(R.string.pref_language), "");
        if (! "".equals(lang) && ! config.locale.getLanguage().equals(lang))
        {
            locale = new Locale(lang);
            Log.i("Locale" , lang);
            Locale.setDefault(locale);
            config.locale = locale;
            getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
        }
    }
}

I have got to change the value of settings.getString(getString(R.string.pref_language), ""); from the Settings Activity of my app.

The problem is none of the strings (which have a translation) and are accessed using getString() method, are showing the translated text.

EDIT1 : I am using Android 8.0

Sparker0i
  • 1,787
  • 4
  • 35
  • 60

2 Answers2

1

In android 8, the behavior of the Locales changed, now every Activity context will take its locale, i.e if you in Activity1 and changed the locale for Activity1, then Activity2 will remain on the default locale.

The solution is to change the locale for every Activity and your issue will be fixed.

Khalid Taha
  • 3,183
  • 5
  • 27
  • 43
0

When you are using getString(R.string.pref_language) the call will return the string from the language matching Locale.getDefault() as long as a translation is provided.

If a translation is not provided it will return the default language, that is the language your using in the values/strings.xml file.

All translated values are in values-xx folders, where xx is the 2 letter ISO code for the language.

D.J
  • 1,439
  • 1
  • 12
  • 23
Ove Stoerholt
  • 3,843
  • 4
  • 25
  • 33
  • `Log.i("Locale" , lang);` returns the 2 digit code of the language I set (hi), and I have also provided the translations for the strings referenced with `getString()` , but I am still not getting the translated values – Sparker0i Nov 23 '17 at 22:12
  • Strange... I remember when I first had a similar problem. I am from Norway and here we have 2 written languages, nynorsk and bokmal. At first I tried to provide a translation for no, but what I needed to provide was translations for nb (bokmal) and nn (nynorsk). Could you be facing something similar? – Ove Stoerholt Nov 23 '17 at 22:17