0

I'm trying to programmatically change the locale on android using following solution:

mConfiguration = new Configuration(getApplicationContext().getResources().getConfiguration());
mConfiguration.setLocale(new Locale("es"));

Then use this configuration when I want a string resource, like below:

public static String getString(int id)
{
    return getApplicationContext().createConfigurationContext(mConfiguration).getResources().getString(id);
}

I do have separate folder for each language I want to use and I'm getting language specific strings whenever I call the above getString(R.string.thisPerson) function. I have thisPerson string in all the locale specific strings.xml files.

However, whenever I refer a string resource directly in a layout file something like below:

    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/black"
    android:text="@string/thisPerson"
    android:layout_marginRight="10dp"
    />

The above string resource does not respect the locale. I always get the default string (from the default strings.xml file) and not the one from the current locale. What could I be missing, so that I'm not getting the locale specific string in View Layout, but get it when I call the getString() function ?

chabu
  • 111
  • 1
  • 7

2 Answers2

0
Locale locale = new Locale("ru");
Locale.setDefault(locale);
Configuration config = getBaseContext().getResources().getConfiguration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());

You can see more details from here

update

if you need to support higher API levels, you can use this solution instead: link

Anton Makov
  • 825
  • 2
  • 9
  • 25
0
  1. Create a Value folder in res by selecting the local language in which you want translation
  2. Create a string file in this folder and add all your translated string in to it
  3. Find an "Application" class create by you
  4. Inside the Application class create static instance of Local class and initialize it in to onCreate() method of Application class
  5. Use this method given below to change language configuration.

    public void setLocale() {

    if (AppClass.appPreferance.getValueFromAppPreferance(AppPreferance.APP_LNG).equals("") ||
            AppClass.appPreferance.getValueFromAppPreferance(AppPreferance.APP_LNG).equalsIgnoreCase("en")) {
    
        AppClass.appPreferance.setValueInAppPreferance(AppPreferance.APP_LNG, "en");
        AppClass.myLocale = Locale.US;
    
    } else if (AppClass.appPreferance.getValueFromAppPreferance(AppPreferance.APP_LNG).equals("de")) {
        AppClass.appPreferance.setValueInAppPreferance(AppPreferance.APP_LNG, "de");
        AppClass.myLocale = Locale.GERMAN;
    }
    
    if (AppClass.myLocale != null) {
        Locale.setDefault(AppClass.myLocale);
        android.content.res.Configuration configuration = new android.content.res.Configuration();
        configuration.locale = AppClass.myLocale;
        getBaseContext().getResources().updateConfiguration(
                configuration,
                getBaseContext().getResources().getDisplayMetrics());
    }
    
    
    Intent i = getBaseContext().getPackageManager()
            .getLaunchIntentForPackage(getBaseContext().getPackageName());
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(i);
    overridePendingTransition(0, 0);
    

    }

  6. Create following method in appclass in onCreate() method to change language even after app killed and opend

    if (appPreferance.getValueFromAppPreferance(AppPreferance.APP_LNG).equals("") ||
            appPreferance.getValueFromAppPreferance(AppPreferance.APP_LNG).equalsIgnoreCase("en"))
    {
    
        appPreferance.setValueInAppPreferance(AppPreferance.APP_LNG, "en");
        myLocale =Locale.US;
    
    } else if (appPreferance.getValueFromAppPreferance(AppPreferance.APP_LNG).equals("de")) {
        appPreferance.setValueInAppPreferance(AppPreferance.APP_LNG, "de");
        myLocale = Locale.GERMAN;
    }
    
    if (myLocale != null) {
        Locale.setDefault(myLocale);
        android.content.res.Configuration configuration = new android.content.res.Configuration();
        configuration.locale = myLocale;
        getBaseContext().getResources().updateConfiguration(
                configuration,
                getBaseContext().getResources().getDisplayMetrics());
    }
    
  7. Here appPreferance is a instance of global preferance class used to store and access value globally

Jay Thummar
  • 2,281
  • 1
  • 14
  • 22