How to localize some strings from a HTML file that is displayed in a UIWebView controller under iOS?

久未见 提交于 2019-12-11 10:16:22

问题


I am looking for a way that would allow me to localize some strings 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.


回答1:


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.




回答2:


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!



来源:https://stackoverflow.com/questions/6999937/how-to-localize-some-strings-from-a-html-file-that-is-displayed-in-a-uiwebview-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!