Javascript 如何读取Localizable.strings的内容并将其放入UIWebView
在我的应用程序中,我有一个文本文件,其中包含以下内容:Javascript 如何读取Localizable.strings的内容并将其放入UIWebView,javascript,objective-c,ios,uiwebview,localizable.strings,Javascript,Objective C,Ios,Uiwebview,Localizable.strings,在我的应用程序中,我有一个文本文件,其中包含以下内容: <html> <style rel="stylesheet" type="text/css"> body { font-family: Arial, sans-serif; font-size: 18px; color: #333333; padding: 14px; tex
<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>
FIRST_STATEMENT = "This is the first statement";
SECOND_STATEMENT = "This is the second statement";
// 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];
现在的问题是,如何从Localizable.strings访问数据,以便将它们放到UIWebView对象的内容中?我正在考虑javascript,但我不知道如何实现它。有什么想法吗?可以尝试制作html文件模板并将 %@ 在您希望本地化字符串的位置/标记处,然后将模板文件放入App Bundle/Documents目录中并使用- NSString*anHtmlStr=[[NSBundle mainBundle]路径用于资源:@“templateFile” 类型:@“html”]//如果文件在应用程序包中,请使用 //NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,是) anHtmlStr=[NSString STRINGWITHCONTENTS文件:anHtmlStr编码:NSUTF8STRING编码错误:&err] anHtmlStr=[NSString stringWithFormat:anHtmlStr,NSLocalizedString(@“key”,@“comment”) [webView loadHTMLString:anHtmlStr baseURL:baseURL]
根据UIWebView判断,我假设您正在使用xcode和objective-c。您可以这样做:
<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>
FIRST_STATEMENT = "This is the first statement";
SECOND_STATEMENT = "This is the second statement";
// 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];
//消息正文
NSString*htmlString=[NSString stringWithFormat:
@""
“”
“%@”//first语句。
“
”
“%@”//second语句
“
”
“%d”//某个整数值
“”
"",
NSLocalizedString(@“这是第一个语句”,无),
NSLocalizedString(@“这是第二个语句。”,无),
intSomeValue];
可以将字符串拆分为多行。顺便说一句,如果你有很多文本,那么你真的需要注释行来跟踪哪个%@表示什么字符串替换值