2013-04-24 157 views
3

我正在建立一個REST API(這支持國際化)的前端網站,我不知道哪條路要走國際化。我已經研究過js和html解決方案,但它們似乎都不如服務器端選項。i18n靜態HTML內容

鑑於大多數頁面包含只需要本地化支持的靜態內容,jsp將是一個很好的解決方案嗎? jsf看起來好像過火了。

回答

-1

1)如果你只有靜態內容創建不同的HTML文件與 不同的語言,並把它放在單獨的文件夾,並訪問該網站 像

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

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

2)JSP也如果你有動態操作,這是一個不錯的選擇有一個 好的選擇來維護國際資源界限。

我建議使用選項1

+3

選項1的問題在於維護工作量對於每種附加語言呈指數級增長,並且沒有任何措施可以在文件的不同語言版本之間實施一致性。 – 2013-04-24 06:05:54

+0

嗯,選擇選項1)還是2)取決於開發商的情況,要求和舒適性 – Madhu 2013-04-24 06:32:32

2

我去真的不能推薦具有不同的HTML文件。可定位性最佳實踐建議將代碼與翻譯分離。

我知道的最快,最簡單,阻礙性最小的方法是使用Google ARB。考慮以下示例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> 

現在需要提取可本地化的內容。這是可能做到這一點無論是使用extractor tool 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> 

然後讓我們創建這些消息的資源文件,還提供翻譯:

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" 
      } 
     } 
     } 
    } 
); 

最後,將JS添加到HTML中。另外,提供一種簡單的方法從URL中獲取選定的語言環境;即./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> 

此示例的代碼可以發現here

+0

這是一個前端解決方案嗎?它的性能與服務器端解決方案相比如何? – 2013-04-26 01:28:53

+0

通過每種語言的翻譯編譯的HTML優於此。假設,動態獲取翻譯將是最慢的。顯然,這些是猜測而非基準的事實結果。 – Shervin 2013-04-26 01:37:22

+0

您能否告訴我如何導入它?我應該輸入一個ascript嗎? – 2017-03-30 15:54:29