How to localize some strings from a HTML file that

2019-08-01 02:49发布

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.

2条回答
不美不萌又怎样
2楼-- · 2019-08-01 03:24

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!

查看更多
We Are One
3楼-- · 2019-08-01 03:27

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.

查看更多
登录 后发表回答