WebView language error Android N

不羁岁月 提交于 2021-02-08 07:57:21

问题


I am aware of this question asked before about the WebView being broken in Android N especially with localization. I have another problem which I cannot figure out how to fix.

As shown in pic, I have two languages set on my Pixel device running 7.1.1. Now, I'm trying to load the following url in a WebView in my app.

"https://accounts.google.com/ServiceLogin?<my-params>"

What I notice is that the page loads in Chinese instead of English which is my current Locale language. I tried opening the above link on my Chrome desktop (where I am signed in with the same account as my phone) and it loads in Chinese too! I went to Chrome's settings to find that they language preferences are saved to my account because they show up in too.

I did try setting locale before setContentView() in my app like this:

public static void setLocale(Locale locale){
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.setLocale(locale);
    Context context = MyApplication.getInstance();
    context.getResources().updateConfiguration(config,
            context.getResources().getDisplayMetrics());
}

but in vain. If I put a log statement, it prints the correct Locale which is en-US. The WebView still loads the page in Chinese though. Any thoughts on how can I fix it?


回答1:


The issue is affected by the webview in the Android N.

  • On the first launching of the webview, it resets the locale to default.
  • If you then rotate the phone - the locale gets correctly set back to the custom locale.
  • If you then launch the webview again - the locale stays correct.

To fix this, you have to switch back to your chosen language after the page was loaded.

You may also check on the Activity/Fragment life cycle when to switch language.

Sources:

https://issuetracker.google.com/issues/37113860 https://gist.github.com/amake/0ac7724681ac1c178c6f95a5b09f03ce#new-locales-vs-old-locales-chinese Activity is blinking after locale change in Android 4.1+



来源:https://stackoverflow.com/questions/42447429/webview-language-error-android-n

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