0

today I'm stuck with swapping language. I want to instantly change the language for my app when I click on a button.

I have two button, one for FR the other for ENG. I thought I could just use another strings.xml when clicked but after some research it's all about Location and stuff, and from what I understood it chose the correct language depending on the language phone.

Is there a solution to swap language when button is clicked just by using another strings.xml file?

Jonathan Aurry
  • 93
  • 1
  • 10
  • 1
    Possible duplicate of [Change language programmatically in Android](http://stackoverflow.com/questions/2900023/change-language-programmatically-in-android) – Zelldon Dec 12 '16 at 10:25
  • 2
    I know but I read others asked question and it didn't really helped me. – Jonathan Aurry Dec 12 '16 at 14:31

3 Answers3

6

copy the following class into your app

public class LocaleHelper {

    private static final String SELECTED_LANGUAGE = "Locale.Helper.Selected.Language";

    public static void onCreate(Context context) {

        String lang;
        if(getLanguage(context).isEmpty()){
            lang = getPersistedData(context, Locale.getDefault().getLanguage());
        }else {
            lang = getLanguage(context);
        }

        setLocale(context, lang);
    }

    public static void onCreate(Context context, String defaultLanguage) {
        String lang = getPersistedData(context, defaultLanguage);
        setLocale(context, lang);
    }

    public static String getLanguage(Context context) {
        return getPersistedData(context, Locale.getDefault().getLanguage());
    }

    public static void setLocale(Context context, String language) {
        persist(context, language);
        updateResources(context, language);
    }

    private static String getPersistedData(Context context, String defaultLanguage) {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        return preferences.getString(SELECTED_LANGUAGE, defaultLanguage);
    }

    private static void persist(Context context, String language) {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = preferences.edit();

        editor.putString(SELECTED_LANGUAGE, language);
        editor.apply();
    }

    private static void updateResources(Context context, String language) {
        Locale locale = new Locale(language);
        Locale.setDefault(locale);

        Resources resources = context.getResources();

        Configuration configuration = resources.getConfiguration();
        configuration.locale = locale;

        resources.updateConfiguration(configuration, resources.getDisplayMetrics());


    }
}

Then if you want to change the language , then change it like this

LocaleHelper.setLocale(this,"fr") //for french;

LocaleHelper.setLocale(this,"en") //for english;

Make sure you have separate values folder like for french values-fr and you have separate string resources for both language.

Mithun Sarker Shuvro
  • 3,902
  • 6
  • 32
  • 64
1

use this code on button click:

String languageToLoad  = "fr";
         Locale locale = new Locale(languageToLoad); 
         Locale.setDefault(locale);
         Configuration config = new Configuration();
         config.locale = locale;
         context.getResources().updateConfiguration(config,context.getResources().getDisplayMetrics());

    Intent intent = new Intent(XYZ.this, XYZ.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);
Rishabh Mahatha
  • 1,251
  • 9
  • 19
0

To build multilingual Android apps you need to collect the texts into resource files and translate them.

Once you provide the translation, Android OS will choose the resources that match user’s locale. If your application is available in several languages, Android will select the language that the device uses.

Application Localization Process

Android loads text and media resources from the project’s ‘res’ directory. Additionally, Android can select and load resources from different directories, based on the current device configuration and locale.

For example, if the code loads a string called ‘R.string.title’, Android will choose the correct value for that string at runtime by loading the appropriate strings.xml file from a matching ‘res/values’ directory.

In order to have a multilingual Android app you need to provide Android with the localized resource files.

If the locale is ‘en-US’, Android will look for a value of “R.string.title” by searching the files in the following order:

‘res/values-en-rUS/strings.xml’
‘res/values-en/strings.xml’
‘res/values/strings.xml’

When it manages to find the string in the resource file, it uses that value and stops searching. This means that the default language acts as a fall-back option and when translation exists, it’s used instead. It also means that you should either localize everything or nothing, as seeing half translated applications is usually worse than apps that are not translated at all.

Follow this link to get more on it:

https://www.icanlocalize.com/site/tutorials/android-application-localization-tutorial/

Rohit Sharma
  • 2,017
  • 1
  • 20
  • 22