4

I am using "loadDataWithBaseUrl(...)" to load a html file, stored in assets, to Webview. that contains a string "Loading..." and a rotating GIF. String "Loading..." is hard coded, and it'll not be localized. How to replace that string dynamically, so that it can be localized?

Please help me to resolve this.

Kantesh
  • 875
  • 3
  • 8
  • 19

3 Answers3

3

There are various solutions I could think of :

  • Load a different asset file according to the current language (get the current language using Locale.getDefault()), This way you can translate your HTML files independently.

  • Use place holders in your HTML file (for instance #loading_message#), then load the asset file in a String, replace all the occurences of the placeholder by the appropriate localised message (String.replaceAll("#loading_message#", getText(R.string.loading_message).toString())), finally load the processed HTML into the WebView using the loadData(String data, String mimeType, String encoding) function.

To load the asset file, you can do something like that:

File f = new File("file:///android_asset/my_file.html");        
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);

StringBuffer sb = new StringBuffer();
String eachLine = br.readLine();

while(eachLine != null) {
  sb.append(eachLine);
  sb.append("\n");
  eachLine = br.readLine();
} 

// sb.toString is your HTML file as a String
Vincent Mimoun-Prat
  • 28,208
  • 16
  • 81
  • 124
  • I've tried some ways like this. Then problem will be how to get the content of html file stored in assets. – Kantesh Nov 25 '10 at 09:39
  • Is this a valid answer http://stackoverflow.com/questions/4273688/how-to-get-the-path-of-a-drawable-or-gif-in-android/4273733#4273733 – Kantesh Nov 25 '10 at 09:39
  • If you go the second way, you can use a personal tag (such as ) and feed the html to a parser, as explained here (for iOs): [link]http://stackoverflow.com/questions/6999937/how-to-localize-some-strings-from-a-html-file-that-is-displayed-in-a-uiwebview-c – tos Jan 30 '12 at 11:58
3

I had a similar problem when using the WebView to show help text that should be translated.

My solution was to add multiple translated HTML files in assets and loading them with:

webView.loadUrl("file:///android_asset/" + getResources().getString(R.string.help_file));

For more details go to: Language specific HTML Help in Android

Eric Obermühlner
  • 1,076
  • 9
  • 9
0
String str = "Loading ..."
String newStr = str.substring("Loading ".length());
newStr = context.getResourceById(R.string.loading) + newStr;

I hope the code is sufficiently clear to understand the idea: extract the string without "Loading " and concatenate it with the localized version of "Loading" string

Vladimir Ivanov
  • 42,730
  • 18
  • 77
  • 103
  • I am loading a html content from a file stored in assets folder and I want to replace some string (Loading...) in that html content, as i want. – Kantesh Nov 25 '10 at 09:26
  • you can't modify the content in your assets folder. You may just load the content in memory, modify it and use. – Vladimir Ivanov Nov 25 '10 at 09:32
  • What is th e use o f loadDataWithBaseUrl("url",data,contenttype,encoding,failuerurl); – Kantesh Nov 25 '10 at 09:35
  • I don't really want to change the file content but after loaded my string shoud be displayed. – Kantesh Nov 25 '10 at 09:42