Best practice javascript and multilanguage

后端 未结 9 1877
走了就别回头了
走了就别回头了 2020-11-29 17:37

what is the best practice for multilanguage website using DOM Manipulating with javascript? I build some dynamic parts of the website using javascript. My first thought was

相关标签:
9条回答
  • 2020-11-29 17:59

    You can use a google translator:

    <div id="google_translate_element" style = "float: left; margin-left: 10px;"></div>
    
    <script type="text/javascript">
    function googleTranslateElementInit() {
      new google.translate.TranslateElement({pageLanguage: 'en', layout: google.translate.TranslateElement.InlineLayout.HORIZONTAL}, 'google_translate_element');
    }
    </script>
    
    <script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
    
    </div><input type = "text" style = "display: inline; margin-left: 8%;" class = "sear" placeholder = "Search people..."><button class = "bar">&#128270;</button>
    
    0 讨论(0)
  • 2020-11-29 18:01

    Just found a nice article about i18n in javascript:
    http://24ways.org/2007/javascript-internationalisation

    Although a simple google search with i18n + javascript reveals plenty of alternatives.

    In the end, it depends on how deep you want it to be. For a couple of languages, a single file is enough.

    You could use a framework like Jquery, use a span to identify the text (with a class) and then use the id of each span to find the corresponding text in the chosen language.
    1 Line of Jquery, done.

    0 讨论(0)
  • For Spring bundles and JavaScript there are simple solution: generate i18n array in template (e.g. JSP) and use it in JavaScript:

    JSP:

    <html>
    <script type="text/javascript">
        var i18n = [];
        <c:forEach var='key' items='<%=new String[]{"common.deleted","common.saved","common.enabled","common.disabled","...}%>'>
            i18n['${key}'] = '<spring:message code="${key}"/>';
        </c:forEach>
    </script>
    </html>
    

    And in JS:

    alert(i18n["common.deleted"]);
    

    See also Resolving spring:messages in javascript for i18n internationalization

    0 讨论(0)
提交回复
热议问题