How to localize some lines from the HTML file that is displayed in the UIWebView controller in iOS?

I am looking for a way that would allow me to localize some lines from an HTML file that is displayed in UIWebView under iOS.

I want to use NSLocalizesString() to do localization, so I'm looking for a simple solution that I would like to create a localized html file before displaying it.

I have full control over the HTML file and setting to use some kind of placeholders.

+1
source share
2 answers

Use a custom tag, then parse the HTML with NSXMLParser and everything that is inside your custom tag ... e.g. <localize>Something</localize> - you remove tags, localize the string, and then transfer this HTML code to the web view.

+1
source

I tried to implement this solution by creating a set of localized strings with built-in HTML formatting, and the idea was to combine them to get one line that I would assign to the loadHTMLString:baseURL: UIWebView method.

However, mixing HTML with plain text in localized strings seemed like an expected nightmare in anticipation of future expectation, so instead I tried this:

I wrote a simple test HTML file with English text and copied it into my Xcode project. Then I added Spanish localization for this file. When I tested this on the iPhone Simulator, it worked fine. The line of code that I used to read the contents of the HTML file and assigned it to a UIWebView is as follows:

 NSString *path = [[NSBundle mainBundle] pathForResource:@"help" ofType:@"html"]; [myUIWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]]; 

Having multiple HTML files to support may seem like a lot of work, but it's the same thing you do when localizing nib files, and I think this approach works best when we think about decoupling this type of content from a regular localized string.

Hope this helps!

0
source

All Articles