【问题标题】:How to Read Contents of Localizable.strings and Put it To UIWebView如何读取 Localizable.strings 的内容并将其放入 UIWebView
【发布时间】:2012-10-31 08:16:13
【问题描述】:

在我的应用程序中,我有一个包含以下内容的文本文件:

<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,但我不知道该怎么做。有什么想法吗?

【问题讨论】:

    标签: javascript objective-c ios uiwebview localizable.strings


    【解决方案1】:

    可以尝试制作一个html文件模板并放

    %@

    在您想要本地化字符串的地方/标签处,然后将模板文件放入 App Bundle/Documents 目录并使用它-

    NSString *anHtmlStr = [[NSBundle mainBundle] pathForResource:@"templateFile" ofType:@"html"];//如果文件在 App Bundle 中,则使用

    //NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,是的);

    anHtmlStr = [NSString stringWithContentsOfFile:anHtmlStr 编码:NSUTF8StringEncoding 错误:&err];

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

    [webView loadHTMLString:anHtmlStr baseURL:baseURL];

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-06-19
        • 1970-01-01
        • 1970-01-01
        • 2017-07-27
        • 2019-11-30
        • 1970-01-01
        • 2010-11-02
        相关资源
        最近更新 更多