My android application is in dual language. So I have 2 res folder value and value-sw(for swahili). I am fetching string values from this file in design and also at run time. For example in layout:
android:hint="#string/Officer"
and in code:
getResources().getString(R.string.Officer);
To change the locale setting I have function which I call on onCreate() which looks like this:
public void ChangeLanguage(Context ctx,String Language){
Resources res = ctx.getResources();
DisplayMetrics dm = res.getDisplayMetrics();
android.content.res.Configuration config = res.getConfiguration();
config.locale = new Locale(Language.toLowerCase());
res.updateConfiguration(config, dm);
}
All works fine. Problem comes only if I change the orientation.
For example on start up I set the language to Swahili. So everything is in Swahili. Now if I change the orientation from vertical to horizontal or vice-verse. Textbox box hint which was set in layout remains in Swahili which I expect. But getResources().getString(R.string.Officer); fetches value from the default value file, which is English.
Any suggestions?
Set android:configChanges="orientation" to your activity in AndroidManifest.xml
Doing so will cause your activity not to be destroyed and re-created when orientation is changed.
The problem with having the android:configChanges attribute, is that that disables onCreate() from being called after an orientation change. You are basically telling the app: "I know there will be orientation changes, so don't recreate the screen, I will handle it"
I would suggest calling that function of yours in the onConfigurationChanged() method. Have in mind that any views will be null at that point, so if you need them you will need to get a new reference.
I have same case as one in the question, using English and Swahili in an app.
This is how I tackled it : I created a folder called values-sw then in the folder I created a resource file named string.xml. this is where all my Swahili translations are written. I call the following method in onCreate:
public static void setLocale(Context context) {
SharedPreferences sharedPreferences = context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);
String userLocale = sharedPreferences.getString("userlocale", null); // adapt to your need
if (userLocale != null) {
Locale locale = new Locale(userLocale);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
}
}
Dont forget to refresh your activity for changes to take effect:
finish();
startActivity(getIntent());
(I know it's pretty late for this answer I hope it helps someone out there, cheers!)
Related
I'm trying to change the language of the application according to the user's input. I tried using this code to change the language of the application and it's working pretty fine.
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(MainActivity.this, MainActivity.class);
startActivity(refresh);
}
But the problem is that app has to restart/refresh in order to reload the resources.
Is this the correct approach to set the language of the app programmatically?
Is there any other way to change the language without refreshing the app?
Try with recreate() on your activity. This approach was successful in my case. If you are on Fragment, then use getActivity().recreate();
SharedPreferences.Editor editor = prefs.edit();
editor.putString(Constants.APP_STATE.SAVED_LOCALE, localeString);
editor.apply();
getActivity().recreate();
Override following method of your activity:
#Override
protected void attachBaseContext(Context newBase) {
SharedPreferences prefs = newBase.getSharedPreferences(Constants.APP_STATE.STATE_SHARED_PREFERENCES, MODE_PRIVATE);
String localeString = prefs.getString(Constants.APP_STATE.SAVED_LOCALE, Constants.DEFAULTS.DEFAULT_LOCALE);
Locale myLocale = new Locale(localeString);
Locale.setDefault(myLocale);
Configuration config = newBase.getResources().getConfiguration();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
config.setLocale(myLocale);
if(Build.VERSION.SDK_INT > Build.VERSION_CODES.N){
Context newContext = newBase.createConfigurationContext(config);
super.attachBaseContext(newContext);
return;
}
} else {
config.locale = myLocale;
}
super.attachBaseContext(newBase);
getResources().updateConfiguration(config, getResources().getDisplayMetrics());
When user set wanted locale, what you want to do is to save it into some string in SharedPreferences and call recreate() of activity. This will then call attachBaseContext(Context context) and in this method proper locale will be set to configuration, then new context will be created with this configuration. After that, new context will be sent to super class which will update application context and proper locale will be shown.
It is also good because locale is automatically set when starting app next time.
Change locale is a Configuration which leads your acitivity restart. There are many other things have the same effect like orientation , keyboardHidden. Take care of your activity's states.
If restarting your activity requires that you recover large sets of
data, re-establish a network connection, or perform other intensive
operations, then a full restart due to a configuration change might be
a slow user experience. Also, it might not be possible for you to
completely restore your activity state with the Bundle that the system
saves for you with the onSaveInstanceState() callback—it is not
designed to carry large objects (such as bitmaps) and the data within
it must be serialized then deserialized, which can consume a lot of
memory and make the configuration change slow. In such a situation,
you can alleviate the burden of reinitializing part of your activity
by retaining a Fragment when your activity is restarted due to a
configuration change. This fragment can contain references to stateful
objects that you want to retain.
Not the best-practice, but you can handle it
<activity android:name=".MyActivity"
android:configChanges="locale"
android:label="#string/app_name">
Then in your Activity
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Do your stuff here
}
Remember to take care of your state. Using setRetainFragment(true) is a good approach. Read this
Yes your code is correct and if you want without refreshing the application
then In Your Application class you need to call this in onCreate() method
String languageSelected = "en";//selected language
Locale myLocale = new Locale(languageSelected);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
getActivity().onConfigurationChanged(conf);//Call this method
You need to perform this particular language to be selected on Application class.
I'm having a problem where if I open my android app and then go into the system settings to change the language and then open the app again, some strings in the app won't be translated unless the app is force quit and relaunched. Any idea why this happens? I don't have android:configChanges set to "locale" anywhere in my AndroidManifest, so doesn't this mean all activities should be restarted on their own?
I also had this issue.I used the code below which was posted in some StackOverflow answer and then it changed the language without refreshing the activity
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);
onConfigurationChanged(conf);
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
// refresh your views here
lblLang.setText(R.string.langselection);
super.onConfigurationChanged(newConfig);
}
I hope it would help you.......
Finally figured out the problem. The strings that weren't being translated were being populated within a static class. So because this class was only being instantiated once, the strings weren't getting re-populated again with the proper translations.
Is it possible to translate android application at runtime using XML received from service?
If it is possible could someone please point me in right direction.
Thank you.
Warning
Everything I have read says that its not a good idea to let your app change the language because it isn't supported by the Android framework and it may cause problems.
With that being said, I have done it in my app and, while it has been a bit of a pain, it seems to be working so far. Here is how I did it in case you want to do it this way. You need a separate strings.xml file for each language. strings.xml in values folder as your default then maybe a strings.xml in say a values-es folder for Spanish strings. I have used the following code to change the configuration settings depending on a RadioButton that the user selects
final Configuration LANG_CONFIG = ChooseLocale.this.getResources().getConfiguration();
Locale newLocale = new Locale("English");
curLang = ChooseLocale.this.getLanguage();
if ((curLang.equals("English")) || (curLang.equalsIgnoreCase("Ingles")))
{
newLocale = new Locale("en_US");
}
else
{
newLocale = new Locale("es");
}
Toast.makeText(getApplicationContext(), newLangToast + " " + curLang , Toast.LENGTH_SHORT).show();
Configuration config = getBaseContext().getResources().getConfiguration();
Locale.setDefault(newLocale);
config.locale = newLocale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
SharedPreferences.Editor editor = langPref.edit();
editor.putString(LANG_PREF, curLang);
editor.commit();
With this line being the most important to update the config
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
I get the locale from my getLanguage() function which can be handled however you want. I also had to add
#Override
public void onConfigurationChanged(Configuration newConfig) {
newConfig = Globals.getUserLanguage(this);
super.onConfigurationChanged(newConfig);
to every activity that allows orientation change and add this to my onCreate() of each
final SharedPreferences langPref = getSharedPreferences (LANG_PREF, 0);
if (Globals.langConfig != null)
this.onConfigurationChanged(Globals.langConfig);
I also added android:configChanges="orientation|locale" to each activity in the manifest that allows orientation change
Android Docs on localization
I would like the application language to be set according to the user preferences but up till now it doesn't work how I would like it to.
I have set the default values: strings.xml and also values-es with a strings.xml inside in spanish. I have a menu option which brings the user to a Preference activity where he can amon gother things chose the language.
So here are some extracts of the code:
public class Preference extends PreferenceActivity implements
OnSharedPreferenceChangeListener {
......
// Set up a listener whenever a key changes
getPreferenceScreen().getSharedPreferences()
.registerOnSharedPreferenceChangeListener(this);
...}
//(......)
//and here I have the listener so when the language pref changes value the locale gets changed.
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
if (key.equals("listPref2")) {
String idioma = sharedPreferences.getString("listPref2", "catala");
if ("castella".equals(idioma)) {
idioma = "es_ES";
Locale locale = new Locale(idioma);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getApplicationContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
}
}
}
So when I change the language it works but then when I come back later or restart the emulator the language gets back to default locale the en_US and the app language gets changed back to default again. What can I do to sort that out?
I know I can get this preference (which I can access to from all my activities) and then each time set up the locale but I find it a bit heavy isn't there a way to do it in a more elegant way?
What I would like to do is if the user sets up the language so when he comes back 2 days later he doesn't have to change the language again.
Any ideas?
OK it may help someone. I have added the folowing to the main activity manifest:
android:configChanges="locale"
Then when the user choses the preferences I have put a confirm button and then this button brings you to main activity that is why the lnagages gets reset.
I have a static class where I have this code to change the locale:
public static void updateLanguage(Context context, String idioma) {
if (!"".equals(idioma)) {
if ("castella".equals(idioma)) {
idioma = "es_ES";
} else if ("catala".equals(idioma)) {
idioma = "ca_ES";
}
Locale locale = new Locale(idioma);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
context.getResources().updateConfiguration(config, null);
}
}
end at every activity I have like 20 of them I call this method before:
setContentView(R.layout.list_event);
With these methods when I rotate the screen the activities don't change the language
here is a link to a blog that helped me:
http://adrianvintu.com/blogengine/post/Force-Locale-on-Android.aspx
I would think that you need to be setting the locale in the MainActivity onCreate method. The same way you are setting it when the onSharedPreferenceChanged method.
So I have a language setting in my application. When the language is switched, I would like all the textviews etc to change language immediately. Currently I just change the locale in the configuration, so the language has changed when the user restarts the activity.
An ugly solution to my problem would be to make each textview load the new resources each time the language is changed. Is there a better solution? Perhaps a neat way to discretely restart the activity? Or maybe just force reload of the resources?
In your AndroidManifest.xml, add this attribute to your Activity
android:configChanges="locale"
In your activity override onConfigurationChanged()
#Override
public void onConfigurationChanged(Configuration newConfig) {
// refresh your views here
super.onConfigurationChanged(newConfig);
}
https://developer.android.com/guide/topics/manifest/activity-element.html#config
I think the question is switching language in runtime for the app and displaying localized messages in UI. android:configChanges="locale" calls onConfigurationChanged if the system locale is changed (in setting of your device) while the app is running, and not if you change locale in the code for your app, which I guess is what you want to accomplish. That's why it's not refreshing.
Here is the method I use during every activity onCreate() or onResume() depending on my needs (if my activity will be resuming after user changed language settings or will always be created with language already set):
From there I just refresh the view manually or from onConfigurationChanged() which get called after this method finishes.
public static void changeLocale(Activity activity, String language)
{
final Resources res = activity.getResources();
final Configuration conf = res.getConfiguration();
if (language == null || language.length() == 0)
{
conf.locale = Locale.getDefault();
}
else
{
final int idx = language.indexOf('-');
if (idx != -1)
{
final String[] split = language.split("-");
conf.locale = new Locale(split[0], split[1].substring(1));
}
else
{
conf.locale = new Locale(language);
}
}
res.updateConfiguration(conf, null);
}
I'm not sure why this isn't picked up by onConfigurationChanged().
Hey, sandis, do you mean the method onConfigurationChanged() doesn't called in your activity when you changed the language? I met the same problem. The problem maybe this: when we change the language, the activity goes to onDestroy()(you can try this), so there is nobody to call onConfigurationChanged(). When we launch the activity again, the onCreate() is called, not the onConfigurationChanged(). There maybe something different in locale change and orientation change.
public void settingLocale(Context context, String language) {
Locale locale;
Configuration config = new Configuration();
if(language.equals(LANGUAGE_ENGLISH)) {
locale = new Locale("en");
Locale.setDefault(locale);
config.locale = locale;
}else if(language.equals(LANGUAGE_ARABIC)){
locale = new Locale("hi");
Locale.setDefault(locale);
config.locale = locale;
}
context.getResources().updateConfiguration(config, null);
// Here again set the text on view to reflect locale change
// and it will pick resource from new locale
tv1.setText(R.string.one); //tv1 is textview in my activity
}
Assuming you're changing the language through something like
private void updateLocale(#NonNull final Context context,
#NonNull final Locale newLocale) {
final Resources resources = context.getResources();
final DisplayMetrics displayMetrics = resources.getDisplayMetrics();
final Configuration configuration = resources.getConfiguration();
configuration.locale = newLocale;
resources.updateConfiguration(configuration, displayMetrics);
Locale.setDefault(newLocale);
}
You'll need to call Activity.recreate() in all currently open activities, which is what would happen if the user changed the system language while you were not subscribing to android:configChanges="locale".