Question

I have an Android application with an embedded WebView. I need to apply custom language settings for web-pages presented via this WebView. I found this related answer and gave it a try.

Specifically, the code is:

Map<String, String> headers = new HashMap<String, String>();
String lang = Locale.getDefault().getLanguage(); // for example, returns "ru"
headers.put("Accept-Language", lang); // also tried "ru", "ru_RU", "ru;q=0.8,en;q=0.6", etc.
mWebView.loadUrl("http://localhost/", headers);

Nothing works. At the server side I always get the header with "en_us".

According to the Android documentation:

additionalHttpHeaders - the additional headers to be used in the HTTP request for this URL, specified as a map from name to value. Note that if this map contains any of the headers that are set by default by this WebView, such as those controlling caching, accept types or the User-Agent, their values may be overriden by this WebView's defaults.

The problematic part is highlighted in bold.

So the question is - how to override WebView's defaults to meet preferred user language?

Currently the problem happens in Android 2.3, and I did not yet check it on other versions.

Was it helpful?

Solution

After some investigation it turned out to be a bug in the server-side framework Yii, which does not properly sort preferred languages when they are mentioned in the Accept-Language header with the same priority. For example, if two languages are given with omitted weightes (meaning 1.0 by default), there is no guarantee that Yii will choose the first one. In my case it took the second. Unfortunately, the fix of the bug was eventually eliminated from the trunk.

As for Android, it seems always passing the http header according to current user language chosen in the system settings. It's ok for most of use cases, so there is no need to (try to) override the header.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top