Some strings are not translated when changing system language without restarting app

强颜欢笑 提交于 2019-12-11 11:17:11

问题


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?


回答1:


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.......




回答2:


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.



来源:https://stackoverflow.com/questions/17659884/some-strings-are-not-translated-when-changing-system-language-without-restarting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!