0

my app is using a WebView to display some info from a web server. As I'm supporting multiple languages, such as Portuguese, I noticed that if an error occurs while loading the required page into the WebView and the phone language is set to Portuguese, the WebView will display an error page in English and not Portuguese as I would expect.

Would anyone know why and how could I fix this without catching the error and loading a custom error page?

Thank you.

nyarlathotep77
  • 181
  • 1
  • 9

1 Answers1

1

There are in fact two kinds of error pages that WebView can display. The first kind are the error pages generated by WebView itself. These pages are displayed when WebView for some reason can't connect to the server, for example, if you try navigate while the device is in Aeroplane mode. These pages are localized--at least on KitKat and Lollipop where I've just checked, but I'm pretty sure on earlier versions also. The only issue is that instead of a readable error description WebView displays a cryptic error tag, e.g. "net::ERR_NAME_NOT_RESOLVED", which is indeed not localized (in fact, this is just the name of a constant in Chromium code). You can display your own error page if you override WebViewClient.onReceivedError callback.

Now, there is also the second kind of error pages--the error pages received from servers, e.g. "404 Not found" or "500 Server error". Localization of these pages is servers' responsibility. The only caveat is that the server needs to somehow know the language that you want it to use (e.g. via a cookie, or a query parameter, that largely depends on the server you use). If a server doesn't provide sending localized error pages, there is not much you can do about it, as WebView doesn't actually tell you, if it has received a page with HTTP error status. The best you can do is reading the page title via WebView.getTitle or retrieving the page contents (which is unfortunately also non-trivial, see how to get html content from a webview?)

Community
  • 1
  • 1
Mikhail Naganov
  • 6,643
  • 1
  • 26
  • 26
  • Hi, thanks for your answer. I'm talking about the first type of errors that you mentioned, the ones generated by WebView. This happens on Lollipop. I need to check with a sample app then to trying isolate the issue. Thank you. – nyarlathotep77 Mar 15 '15 at 07:20