Use different strings.xml files regardless of Locale

2

I would like to know if, without changing the Locale of the app, there is some possibility to choose which file of strings.xml to use in the app.

For example, if I have a file of strings.xml and when translating this I have created a new strings.xml(en) , do that, if I want it, when the Locale is es_ES use the second file, that is, the string.xml (en).

Is it possible to do this? If it is not, would it be worth changing the Locale to use one resource or another?

From what I've read, it's supposed to be. However, I have done it and it does not work.

Code:

 Locale aLocale = new Locale.Builder().setLanguage("en").setRegion("US").build();
                    Locale.setDefault(aLocale);
                    getResources().getConfiguration().setLocale(aLocale);

 Locale current = getResources().getConfiguration().locale;

   Toast.makeText(getApplicationContext(), Locale.getDefault().getCountry()+Locale.getDefault().getLanguage(), Toast.LENGTH_LONG).show(); //muestra USen
    Toast.makeText(getApplicationContext(), Locale.getDefault().getDisplayLanguage(), Toast.LENGTH_LONG).show();//muestra English
    Toast.makeText(getApplicationContext(),current.toString(), Toast.LENGTH_LONG).show();//muestra en_US

Despite these results, the text does not change.

    
asked by pepito 25.07.2017 в 10:01
source

1 answer

0

The problem you're having but I'm wrong is that you're not saving your chosen language settings so you're not taking the strings right.

To do this, try saving the configuration in the following way:

String language = "en";

Configuration config = context.getResources().getConfiguration();
Locale sysLocale = null;

//obtenemos la configuración del idioma actual
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    sysLocale = getSystemLocale(config);
} else {
    sysLocale = getSystemLocaleLegacy(config);
}

//comprobamos si la configuración actual del idioma es distinta a la que queremos cambiar.
if (!language.equals("") && !sysLocale.getLanguage().equals(language)) {
    Locale locale = new Locale(language);
    Locale.setDefault(locale);

    //Actualizamos el locale
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
           setSystemLocale(config, locale);
    } else {
           setSystemLocaleLegacy(config, locale);
    }

    //Actualizamos la configuración
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
           context = context.createConfigurationContext(config);
    } else {
          context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
    }
}

This code is prepared for recent and previous versions of Android, you can take a look at this publication of OS in the which indicates how to do it and explains how to do a wrapper to not have to be changing the configuration in each activity.

If you do not want to use a Wrapper, you can change the language within a class that extends the Application and define the language configuration there, so that you do not change it in all the activities.

I think with this you can solve your problem.

    
answered by 25.07.2017 в 10:27