How to Read Contents of Localizable.strings and Put it To UIWebView
-
22-07-2021 - |
Frage
In my app, I have a text file that contains something like this:
<html>
<style rel="stylesheet" type="text/css">
body {
font-family: Arial, sans-serif;
font-size: 18px;
color: #333333;
padding: 14px;
text-align: justify;
}
h2 {
font-size: 18px;
}
</style>
<body>
<p>
This is the first statement.
<br>
This is the second statement.
</p>
</body>
</html>
This data will be loaded to a UIWebView and everything is working. However, I want the texts to be localizable so I'm thinking of putting them to Localizable.strings. And so, Localizable.strings has something like this:
FIRST_STATEMENT = "This is the first statement";
SECOND_STATEMENT = "This is the second statement";
Now, the question is, how I'm going to access the data from Localizable.strings so that I can put them to the contents of the UIWebView object? I'm thinking of javascript but I'm not sure how to do it. Any ideas?
Lösung
Can try making a html file Template and put
%@
at the Places/Tags where u wanna have localize strings and then put the template file in the App Bundle/Documents Dir and use this-
NSString *anHtmlStr = [[NSBundle mainBundle] pathForResource:@"templateFile" ofType:@"html"];//if the file is in App Bundle else use
//NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
anHtmlStr = [NSString stringWithContentsOfFile:anHtmlStr encoding:NSUTF8StringEncoding error:&err];
anHtmlStr = [NSString stringWithFormat:anHtmlStr,NSLocalizedString(@"key", @"comment")];
[webView loadHTMLString:anHtmlStr baseURL:baseURL];
Andere Tipps
I assume you are working with xcode and objective-c, judging by the UIWebView. You can do it like this:
// message body
NSString *htmlString = [NSString stringWithFormat:
@"<body>"
"<p>"
"%@" // first statement.
"<br />"
"%@" // second statement
"<br />"
"%d" // some integer value
"</p>"
"</body>",
NSLocalizedString(@"This is the first statement.", nil),
NSLocalizedString(@"This is the second statement.", nil),
intSomeValue];
You can split the string over multiple lines. Btw if you have lots of text then you really need the comments lines to keep track of which %@ means what string-replace value.