1

A task consists of the following: it is needed to give possibility of change of language of interface of application (ru, en, uk, well and on a default will be chosen en) to the user. For realization of this functional it was done:

1) the folders of resources were created: values, values - uk, values - ru, values - en, where the files strings.xml with content translation.

2) by means of Spinner gets out and written down in SharedPreferences language id.

3) At the start of application in onCreate localization is used :

SharedPreferences preferences = context.getSharedPreferences(Constants.APP_PREFERENCES, Context.MODE_PRIVATE);
String lang = preferences.getString(Constants.LANGUAGE, "default");
  if (lang.equals("default")) {
    lang=context.getResources().getConfiguration().locale.getCountry();
  }
  Locale locale = new Locale(lang);
  Locale.setDefault(locale);
  Configuration config = new Configuration();
  config.locale = locale;
  Log.i("Lang change", "Locale=" + locale);
  context.getResources().updateConfiguration(config,
  context.getResources().getDisplayMetrics());

Almost all pulls up and applies, but not all. Namely, in some places (in some dialogues and BaseAdapters, NavigationDrawer) pulls up localization exactly of language of the system. For example, on a device the Ukrainian localization, on a default is English, and the Russian is chosen in an app, then in the total in an app in the higher indicated places pulls up Ukrainian content besides basic Russian.

Broke through to process in onConfigurationChanged():

@Override
public void onConfigurationChanged(Configuration newConfig)
{
    super.onConfigurationChanged(newConfig);
    locale = new Locale(lang);
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    getBaseContext().getResources().updateConfiguration(config, null);
 }

and cleaned from every activity in manifest.xml android: configChanges = "locale".

Did not help.

What can a problem be in?

  • We have similar issue. Have you solved your's? If yes - lepm me please: https://stackoverflow.com/questions/51839689/localization-of-android-application-one-string-resources-for-several-countries/51840358?noredirect=1#comment90635880_51840358 – yozhik Aug 14 '18 at 14:35

1 Answers1

0

I couldn't find the issue in your code.But here is how i change language of app.Maybe it helps you.It works fine;

//get sharedPreferences
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    //get default local for first initialization
    String defaultLanguage = this.getResources().getConfiguration().locale.getLanguage();
    Configuration configuration = new Configuration();
    //check language preference everytime onCreate of all activities, if there is no choise set default language
    Locale newLocaleLanguage = new Locale(sharedPreferences.getString("newLanguagePref",defaultLanguage));
    //finally setdefault language/locale according to newLocaleLanguage.
    Locale.setDefault(newLocaleLanguage);
    configuration.locale = newLocaleLanguage;
    getBaseContext().getResources().updateConfiguration(configuration, getBaseContext().getResources().getDisplayMetrics());

You should write this code to all activities in onCreate method.And you should write to your sharedPreferences with newLanguagePref tag on your setting page.

Eren
  • 2,583
  • 2
  • 31
  • 36