0

私のアプリには、次のような内容を含むテキスト ファイルがあります。

<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を考えているのですが、やり方がわかりません。何か案は?

4

2 に答える 2

0

HTMLファイルテンプレートを作成して置くことができます

%@

文字列をローカライズしたい場所/タグで、テンプレートファイルをApp Bundle / Documents Dirに配置し、これを使用します-

NSString * anHtmlStr = [[NSBundle mainBundle] pathForResource:@ "templateFile" ofType:@ "html"];//ファイルがAppBundleにある場合は、それ以外の場合は

// NSSearchPathForDirectoriesInDomains(NSDocumentDirectory、NSUserDomainMask、YES);

anHtmlStr = [NSString stringWithContentsOfFile:anHtmlStr encoding:NSUTF8StringEncoding error:&err];

anHtmlStr = [NSString stringWithFormat:anHtmlStr、NSLocalizedString(@ "key"、@ "comment")];

[webView loadHTMLString:anHtmlStr baseURL:baseURL];

于 2012-10-31T11:30:56.723 に答える
0

UIWebViewから判断すると、xcodeとobjective-cを使用していると思います。次のように実行できます。

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

文字列を複数の行に分割できます。ところで、たくさんのテキストがある場合は、どの %@ がどの文字列置換値を意味するかを追跡するためにコメント行が本当に必要です。

于 2012-11-05T00:03:43.577 に答える