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.

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

3 Comments

I've tried some ways like this. Then problem will be how to get the content of html file stored in assets.
If you go the second way, you can use a personal tag (such as <localize>) and feed the html to a parser, as explained here (for iOs): [link]stackoverflow.com/questions/6999937/…
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

Comments

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

4 Comments

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.
you can't modify the content in your assets folder. You may just load the content in memory, modify it and use.
What is th e use o f loadDataWithBaseUrl("url",data,contenttype,encoding,failuerurl);
I don't really want to change the file content but after loaded my string shoud be displayed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.