Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android change language in app only: Some strings not translated properly

(I've added an update on the bottom of the post)

I am currently working on a project that will support multiple language. I have written all strings for all languages.

I use BaseActivity, Application class, and LocaleHelper to manage the localization on my app. And then I found this behavior on my app. After I setLocale to another language, Strings in some activities are changed and correct, but NOT in MainActivity even though they have some same strings. I tried to restart app and it's still not working.

I also have another activity which some of the words in activity got translated but all words inside a fragment and a recyclerview didn't get translated at all.

I still couldn't find why it's not translated properly. Can someone help me??

Here's some snippet:

Application class

@Override
public void onCreate() {
    super.onCreate();
    LocaleUtil.setLocale(new Locale(LocaleUtil.with(this).getPreference()));
    LocaleUtil.updateConfig(this,getBaseContext().getResources().getConfiguration());
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    LocaleUtil.setLocale(new Locale(LocaleUtil.with(this).getPreference()));
    LocaleUtil.updateConfig(this, newConfig);
}

BaseActivity

public BaseActivity() {
    LocaleUtil.updateConfig(this);
}

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    initLocale();
    super.onCreate(savedInstanceState);
}

public void initLocale(){
    localeUtil = new LocaleUtil(this);
    localeUtil.setListener(this);
    localeUtil.setLanguageByCode(localeUtil.getPreference());
}

Please help me! THanks!!

(UPDATE)

I found out that I have to setLocale again whenever I want to access Strings.xml for any views in a recyclerview and a fragment. This is totally inconvenient and I'm worried about the performance. I would love to hear a better advice to change language.

like image 337
A. N Avatar asked Oct 17 '22 03:10

A. N


1 Answers

Check this out. Maybe some string is not include. From documentation.

https://developer.android.com/guide/topics/resources/localization.html#using-framework

Suppose that your application's default language is English. Suppose also that you want to localize all the text in your application to French, and most of the text in your application (everything except the application's title) to Japanese. In this case, you could create three alternative strings.xml files, each stored in a locale-specific resource directory:

res/values/strings.xml Contains English text for all the strings that the application uses, including text for a string named title. res/values-fr/strings.xml Contain French text for all the strings, including title. res/values-ja/strings.xml Contain Japanese text for all the strings except title. If your Java code refers to R.string.title, here is what will happen at runtime:

If the device is set to any language other than French, Android will load title from the res/values/strings.xml file. If the device is set to French, Android will load title from the res/values-fr/strings.xml file. Notice that if the device is set to Japanese, Android will look for title in the res/values-ja/strings.xml file. But because no such string is included in that file, Android will fall back to the default, and will load title in English from the res/values/strings.xml file.

I hope it will work.

like image 174
Kamil Kulig Avatar answered Oct 20 '22 21:10

Kamil Kulig