Android: How do I make App Locale work independently of System Locale?
I have a problem related to display language. I was able to change the language inside the application (“en” in English and “ja” in Japanese) independently of the operating system.
However, the
problem is that when the application is in the “ja” state, if the user manually changes the system language (not “en” or “ja”), my application automatically changes the language to the default language (“en”). I want to make my app’s locale independent, so that no matter what language the user manually changes, the app’s language remains the same as the language they logged out of when they logged out.
Edit
There are some useful links, but they still don’t solve my problem. For example:
Change language programatically in Android
Can you give me some advice?
Thanks in advance!
Solution
import java.util.Locale;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.util.DisplayMetrics;
public void setLocale(String lang) {
myLocale = new Locale(lang);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
Intent refresh = new Intent(this, AndroidLocalize.class);
startActivity(refresh);
finish();
}
Added:
@Override
public void onConfigurationChanged(Configuration newConfig) {
newConfig.setLocale(yourLocale);
super.onConfigurationChanged(newConfig);
}
Added (2):
You must set android:configChanges="layoutDirection|locale"
to trigger onConfigurationChanged()
when you change the locale
. < br/>
I don’t quite understand why this is so, maybe there are some RTL
languages ….