Search code examples
javascriptmultilingualmaterial-design-lite

Multilanguage site in Material Design Lite is not working as expected


Fellow developers,

I'm trying to create a multilanguage site with Material Design Lite, in the past I have used this library:

How to Create a Multilingual Application using JavaScript

And works pretty well, it expects a JSON (en.json) like this:

{
  "chooseTime": "Speech type"
}

And it adds all translations, however if you tried to do the same in MDL nothing happens.

The basic html is like this:

<body load="load()"><!--content--></body>

If I hardcode the data it looks like this:

img1

But if I load the translations, it stays empty.

img2

Here is the full example:

var langs = ["en", "es"];

function load(){
    let translate = new Translate();
    let lang = navigator.language || navigator.userLanguage;
    if (!langs.includes(lang))
        lang = "en";
    translate.init(lang);
    translate.process(); 
}

function Translate() {
    //initialization
    this.init =  function(lng){
        this.attribute = "data-tag";
        this.lng = lng;
    }

    //translate 
    this.process = function(){
                _self = this;
                let xrhFile = new XMLHttpRequest();
                //load content data 
                xrhFile.open("GET", "https://fanmixco.github.io/toastmasters-timer-material-design/js/lang/en.json", false);
                xrhFile.onreadystatechange = function ()
                {
                    if(xrhFile.readyState === 4)
                    {
                        if(xrhFile.status === 200 || xrhFile.status == 0)
                        {
                            let LngObject = JSON.parse(xrhFile.responseText);
                            //console.log(LngObject["name1"]);
                            let allDom = document.getElementsByTagName("*");
                            for(let i =0; i < allDom.length; i++){
                                let elem = allDom[i];
                                let key = elem.getAttribute(_self.attribute);

                                if(key != null) {
                                     //console.log(key);
                                     elem.innerHTML = LngObject[key];
                                }
                            }
                        }
                    }
                }
                xrhFile.send();
    }    
}
<script src="https://code.getmdl.io/1.3.0/material.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/getmdl-select@2.0.1/getmdl-select.min.css" rel="stylesheet"/>
<script src="https://cdn.jsdelivr.net/npm/getmdl-select@2.0.1/getmdl-select.min.js"></script>
<link href="https://code.getmdl.io/1.3.0/material.indigo-pink.min.css" rel="stylesheet"/>

<div class="mdl-textfield mdl-js-textfield getmdl-select">
     <input type="text" value="" class="mdl-textfield__input" id="cmbSpeechType" readonly>
     <input type="hidden" value="" name="hiddenSpeechType" id="hiddenSpeechType">
     <label for="cmbSpeechType" class="mdl-textfield__label">Speech type</label>
     <ul for="cmbSpeechType" class="mdl-menu mdl-menu--bottom-left mdl-js-menu">
        <li class="mdl-menu__item" data-val="0">Question of the Day (30s)</li>
     </ul>
  </div>
  
<div class="mdl-textfield mdl-js-textfield getmdl-select">
     <input type="text" value="" class="mdl-textfield__input" id="cmbSpeechTypeTrans" readonly>
     <input type="hidden" value="" name="hiddenSpeechType" id="hiddenSpeechType">
     <label for="cmbSpeechTypeTrans" class="mdl-textfield__label" data-tag="chooseTime"></label>
     <ul for="cmbSpeechTypeTrans" class="mdl-menu mdl-menu--bottom-left mdl-js-menu">
        <li class="mdl-menu__item" data-val="0" data-tag="opt1"></li>
     </ul>
  </div>

The issue happens in the second select after I added this: data-tag="chooseTime" and nothing was loaded.

To be honest, I don't know why since I have other sites working with the plugin and they worked as expected while in this case nothing happened. Also, I tried to add this code after all translations were loaded:

componentHandler.upgradeAllRegistered();

Any idea or suggestion why is not working? Or any translation plugin that works with MDL? Because I couldn't find one and the only partial example that I found it was built with Spring (Java) and I'm not using any server side programming language. Thanks for your ideas.


Solution

  • I found how to fix it, originally, the plugin works in this way:

    <body load="load();">
    </body>
    

    I just moved the load to the end of the page and now it's working:

    <body>
        <script>load();</script>
    </body>