2012-10-31 40 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]; 

你可以將字符串分成多行。順便說一句,如果你有很多文字,那麼你真的需要評論行來跟蹤哪個%@代表什麼字符串替換值。