简体   繁体   English

i18n用于静态html内容

[英]i18n for static html content

I am building a website on the front of a REST API (this supports i18n) and i'm not sure which way to go about internationalization. 我正在REST API的前面构建一个网站(这支持i18n),我不确定哪种方式可以实现国际化。 I have looked into js and html solutions but they all seem inferior to server side options. 我已经研究过js和html解决方案,但它们看起来都不如服务器端选项。

Given that most of the pages contain static content that just needs locale support would jsp's be a good solution? 鉴于大多数页面都包含只需要语言环境支持的静态内容,jsp是一个很好的解决方案吗? jsf seems like overkill. jsf似乎有点矫枉过正。

I really can not recommend having various HTML files. 我真的不能推荐各种HTML文件。 Localizability best-practices recommend separating the translations from the code. 可本地化的最佳实践建议将翻译与代码分开。

The fastest, simplest, and least obstructive method I know is using Google ARB . 我所知道的最快,最简单,最不具阻碍性的方法是使用Google ARB Consider having the following sample HTML: 考虑使用以下示例HTML:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Testing ARB...</title>
    </head>

    <body>
        <h2>This is a test.</h2>
    </body>
</html>

Now it's needed to extract the localizable content. 现在需要提取可本地化的内容。 It's possible to do this either using the extractor tool ARB provides or if your pages are very simple, you can even do it manually: 可以使用ARB提供的提取器工具来执行此操作,或者如果您的页面非常简单,您甚至可以手动执行此操作:

<html>
    <head arb:namespace="test">
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title arb:id="MSG_HTML_TITLE">Testing ARB...</title>
    </head>

    <body>
        <h2 arb:id="MSG_BODY_TEST">This is a test.</h2>
    </body>
</html>

Then let's create the resource file for these messages and also provide the translation: 然后让我们为这些消息创建资源文件,并提供翻译:

arb.register(
    "test", 
    {
    "MSG_HTML_TITLE": "Testing ARB",
    "MSG_BODY_TEST": "This is a test.",
    "MSG_CURR_LOCALE": "...and the selected language is \"{currentLocale}\".",
      "@MSG_CURR_LOCALE": {
        "placeholders": {
          "0": {
            "description": "This variable would show the current locale.",
            "example": "fr"
          }
        }
      }
    }
);

arb.register(
    "test:de", 
    {
    "MSG_HTML_TITLE": "ARB auf Probe",
    "MSG_BODY_TEST": "Das ist ein Test.",
    "MSG_CURR_LOCALE": "...und die ausgewählte Sprache ist \"{currentLocale}\".",
      "@MSG_CURR_LOCALE": {
        "placeholders": {
          "0": {
            "description": "This variable would show the current locale.",
            "example": "fr"
          }
        }
      }
    }
);

Finally, add the JS to the HTML. 最后,将JS添加到HTML中。 Also, provide an easy way to get the selected locale from URL; 另外,提供一种从URL获取所选语言环境的简便方法; ie ./index.html?locale=de ./index.html?locale=de

<html>
    <head arb:namespace="test">
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title arb:id="MSG_HTML_TITLE">Testing ARB...</title>
        <script src="arb/lib/arbcore.js"></script>
        <script src="test.arb"></script> <!-- ARB file w/ translations. -->
    </head>

    <body>
        <h2 arb:id="MSG_BODY_TEST">This is a test.</h2>

        <!-- Get locale from URL and translate page HTML -->
        <script>

            function main(){
                var locale = arb.getParamFromUrl('locale');
                if (!locale){
                    locale = 'en';
                }
                arb.setResourceSelector(locale);

                // JS localization
                var r$ = arb.getResource("test");
                document.write(arb.msg(r$.MSG_CURR_LOCALE, {'currentLocale': locale}));     

                // This should appear after all the translatable HTML content
                arb.localizeHtml();                             
            }

            main();

        </script>

    </body>
</html>

The code for this sample can be found here . 可以在此处找到此示例的代码。

1) If you have only static content create different html files with different languages and put it in separate folder and access the site like 1)如果您只有静态内容,则使用不同的语言创建不同的html文件,并将其放在单独的文件夹中并访问该网站

for English
http://yourdomain.com/en/english_index.html

for French
http://yourdomain.com/fr/french_index.html

2) JSP also a good option if you have dynamic manipulation. 2)如果你有动态操作,JSP也是一个不错的选择。 Have a good option to maintain i18n resource boundle. 有一个很好的选择来维护i18n资源边界。

I Suggest to go with option 1 我建议选择1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM