Multi language page with Javascript or jquery

前端 未结 3 1482
臣服心动
臣服心动 2021-01-03 07:53

Currently I am working on web app that will support several languages. Therefore I prepared table in my database with translations. However, I am not sure how to populate we

3条回答
  •  情话喂你
    2021-01-03 08:31

    Use on every piece of text you want to change according to the language a span HTML tag because you can embed inline on every piece of HTML (contrary to div or p which have a display:block by default). Then for each span use a class with a name starting with a certain pattern, for example:

    Then using jQuery's function .each change every span tag that matches the pattern lang in its class name, according to the selected language.

    I put also here a simple example for you to check.

    var LanguageList = {
      "EN" : "English",
      "ES" : "Español",
      "PT" : "Português"
    };
    
    //languages Objects
    var WORDS_EN = {
      "text1" : "text One",
      "text2" : "text Two"
    };
    
    var WORDS_ES = {
      "text1" : "texto Un",
      "text2" : "texto Dos"
    };
    
    var WORDS_PT = {
      "text1" : "texto Um",
      "text2" : "texto Dois"
    };
    
    
    window.onload = initialize;
    
    function initialize() {
      var $dropdown = $("#country_select");    
      $.each(LanguageList, function(key, value) {
        $dropdown.
          append($("
    div{
      margin: 15px;
    }
    
    
    
    
    /

提交回复
热议问题