When user changes the system locale from US to China. I don't want to change the language from English to Chinese in my app, I expect that my app's language still in English.
May I do this by setting a android attribute in AndroidManifest.xml?
Thanks
I think it would be better to do this once and put your code in your application class
public class MyApplication extends Application {
#Override
protected void attachBaseContext(Context base) {
Resources res = base.getResources();
Locale locale = new Locale("en");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
res.updateConfiguration(config, res.getDisplayMetrics());
super.attachBaseContext(base);
}
}
If you just want the Chinese xml to stay in the app but not plan in using it, you can simply comment all its content, and the App will consider only the English strings
You can use below code to maintain locale as per your requirement,
Locale locale = new Locale("en");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
Related
If I use this code "setLocale("de"); etc." with button click, it's work. But If I put in onCreate, it does't work. Could you fix for me? Thanks.
public void setLocale(String lang) {
Locale locale = new Locale(lang);
Locale.setDefault(locale);
Configuration config =
getBaseContext().getResources().getConfiguration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
}
I am trying to override the locale to use Czech locale no matter what is set in the phone. However, although when I try to get the current locale, it returns czech, but for plurals, it acts like it still takes the real phone locale.
Here is my code:
<application
android:name=".xxx"
android:allowBackup="true"
android:icon="#drawable/logo"
android:label="#string/app_name"
android:theme="#style/Theme.mystyle"
android:configChanges="locale"
>
In application:
String lang = settings.getString("cs", "");
locale = new Locale(lang);
Locale.setDefault(locale);
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
I posted on GitHub simple LocaleAssistant class I just tested with plurals for Slovak and also Czech language, and it worked on the device with default locale set to EN. Just call those two methods from within your custom Application on the member instance of LocaleAssistant.
In the end I found the correct solution. Gray Wolf's solution did not work for me for some reason. Maybe it is all about the API level you develop your app for.
This is the working code. Besides that, it is also necessary to add configChanges:locale to AndroidManifest. This should work on Android 4.0.1 and above.
public class MyApplication extends Application
{
private Locale locale = null;
#Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
if (locale != null)
{
newConfig.locale = locale;
Locale.setDefault(locale);
getBaseContext().getResources().updateConfiguration(newConfig, getBaseContext().getResources().getDisplayMetrics());
}
}
#Override
public void onCreate()
{
super.onCreate();
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
Configuration config = getBaseContext().getResources().getConfiguration();
String lang = "cs";
if (!"".equals(lang) && !config.locale.getLanguage().equals(lang))
{
locale = new Locale(lang);
Locale.setDefault(locale);
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
}
}
}
I am developing an app which supports two languages. There is one ToggleButton to change language.I am using following code to change locale and all works fine.
Resources res = viewDashBoardScreen.getResources(
// Change locale settings in the app.
DisplayMetrics dm = res.getDisplayMetrics();
android.content.res.Configuration conf = res.getConfiguration();
conf.locale = new Locale("es".toLowerCase());
res.updateConfiguration(conf, dm);
Question: How to refresh whole app after language change ?
Currently I am restarting that activity in which language is changed. It works fine but is it good practice ? Or is there any other way to apply language change in app.
Any help would be greatly appreciated..Thanks.
i thing you can call this function whn togal button change it's working for me
private void setLocale(String localeCode){
AppLog.logString(TAG+"set location function: "+localeCode);
locale = new Locale(localeCode);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
getApplicationContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
UserDetail.this.getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
onCreate(null);
}
on toggel buttong change call function this way :
setLocale("en-us");// pass languen in whicy u want to translate
setLocale("ar",savedInstanceStat);
I'm having a headache with the locale thing in my app. So basically my app supports two languages, English and Vietnamese, and user can choose to change the language to be displayed. So I have a SettingActivity like this:
public class SettingsActivity extends SherlockPreferenceActivity implements OnPreferenceChangeListener{
private ListPreference langPref;
private SharedPreferences languagepref;
private String language;
private Locale locale;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
languagepref = PreferenceManager.getDefaultSharedPreferences(this);
Configuration config = getBaseContext().getResources().getConfiguration();
language = languagepref.getString("languageToLoad","en");
locale = new Locale(language);
Locale.setDefault(locale);
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
addPreferencesFromResource(R.xml.preferences_login);
langPref = (ListPreference) findPreference("lang_pref");
if (language.equalsIgnoreCase("vi")){
langPref.setValueIndex(1);
}
else{
langPref.setValueIndex(0);
}
langPref.setOnPreferenceChangeListener(this);
}
#Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
String key = preference.getKey();
if (key.equals("lang_pref")){
String languageToLoad = (String) newValue;
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
SharedPreferences.Editor editor = languagepref.edit();
editor.putString("languageToLoad",languageToLoad );
editor.commit();
recreate();
}
return false;
}
}
In every other activity, I add this in their onCreate method:
SharedPreferences languagepref = PreferenceManager.getDefaultSharedPreferences(this);
Configuration config = getBaseContext().getResources().getConfiguration();
String language = languagepref.getString("languageToLoad","en");
Locale locale = new Locale(language);
Locale.setDefault(locale);
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
And now my app is acting weird: in the SettingActivity, the language is displayed properly whenever I rotate my device, even without the need of adding android:configChanges="locale" in the activity's manifest. However, in every other activity, the language is changed back to English when I rotate the device. I've try adding android:configChanges="locale" in their manifest but it doesn't work. Also, I've tried every solution found on Stackoverflow but none of them works for me, so I'm basically clueless now. Could anyone of you help me point out the mistake I made here? Please do and thanks in advance.
Try adding
android:configChanges="orientation|keyboard|keyboardHidden"
in the manifest for each activity.
Then you should override onConfigurationChanged in your activities, and in this method reload layout (depending orientation). If you still have problems with locale, reload the appropriate locale in onConfigurationChanged.
When I click on the "sub_changelang" button, it should change the program language to French for example. I got the following code to change the locale but I have no idea how to refresh/ pdate the app to change the language to French.
Button cl = (Button) findViewById(R.id.sub_changelang);
cl.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
Locale locale = new Locale("fr_FR");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
}
});
It doesn't work. How can I fix it? I tried to add:
MainActivity.this.getResources().updateConfiguration(config, MainActivity.this.getResources().getDisplayMetrics());
but it didn't work. I also tried:
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
and it didn't work either.
android:configChanges="locale"
is set inside the AndroidMainfest.xml under application -> activity
I am using this code to set locale
String languageToLoad = "fr_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);
here context is application Base Context.
Please also try "fr" instead of "fr_FR" because I am working for Arabic locale and its working fine.
You need to restart your Activity after changing locale.
You can use activity.this.recreate().But it will support from API level 11.