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 ?