How to localize some strings from a HTML file that is displayed in a UIWebView controller under iOS?
I am looking for a way that would allow me to localize some stri开发者_StackOverflow社区ngs from inside a HTML file that is displayed in a UIWebView under iOS.
I want to use NSLocalizesString()
for doing the localization, so I am looking for a simple solution that would like me to generate the localized html file before displaying it.
I do have full control over the HTML file and plant to use some kind of placeholders.
Use a custom tag then, parse the HTML with NSXMLParser, and anything within your custom tag... e.g. <localize>Something</localize>
- you strip the tags, localize the string, then hand this HTML over to your web view.
I tried to implement a solution to this by creating a set of localized strings with embedded HTML formatting, and the idea was to concatenate those to get a single string which then I'd assign to the loadHTMLString:baseURL:
method of UIWebView
.
However, mixing HTML with regular text in the localized strings seemed like a future maintenance nightmare waiting to happen, so instead I tried this:
I wrote a simple test HTML file with English text and copied it to my Xcode project. Then I added a Spanish localization for this file. When I tested this in the iPhone Simulator it worked fine. The line of code I used to read the content of the HTML file and assign it to the UIWebView is this:
NSString *path = [[NSBundle mainBundle] pathForResource:@"help" ofType:@"html"];
[myUIWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]];
Having several HTML files to maintain might seem to be 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 separating this type of content from the regular localized strings.
Hope this helps!
精彩评论