2012-10-31 38 views
0

在我的应用程序,我有一个包含像这样的文本文件:如何阅读Localizable.strings的内容并把它放到一个UIWebView

<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> 

该数据将被加载到一个UIWebView和一切工作。但是,我希望文本是可本地化的,所以我正在考虑将它们放到Localizable.strings中。因此,Localizable.strings有这样的事情:

FIRST_STATEMENT = "This is the first statement"; 
SECOND_STATEMENT = "This is the second statement"; 

现在的问题是,我要如何从Localizable.strings访问数据,这样我可以把他们的一个UIWebView对象的内容?我在想JavaScript,但我不知道该怎么做。有任何想法吗?

回答

0

可以尝试做一个HTML文件模板,并把

%@

在地方/标签,其中u希望有本地化的字符串,然后把模板文件的应用程序包/文件尔和使用这 -

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];

0

我假设你正在使用xcode和objective-c,通过UIWebView来判断。你可以这样做:

// 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]; 

你可以将字符串分成多行。顺便说一句,如果你有很多文字,那么你真的需要评论行来跟踪哪个%@代表什么字符串替换值。