我更改网站语言的代码无效

时间:2018-11-28 20:56:10

标签: javascript json

我已经为我的网站做了一个语言切换器,直到我决定将文本存储在本地.json文件中之前,它一直有效。之所以可行,是因为它在加载页面时会读取文本,但是当您尝试更改语言时,它将不起作用。这是我的JavaScript代码:

/*This is a list of all the id's I want to change*/
let ids = ["titleText", "llenguatge", "runBut", "trainBut", "resultText", "atention", "warn", "coll0but"]

function catalan() {
    /*I load the json file from the folder. This is the part that doesn't work*/
    $.getJSON("scripts/json/text_cat.json", function(cat) {
        console.log(cat); 
        for (let i = 0; i < ids.length; i++) {
            /*For every element with that id, change the text corresponding with that id (in the json file, the title is the same as the id)*/
            document.getElementById(ids[i]).innerHTML = cat[ids[i]]
        }
    });
    /*This part does work*/
    console.log("Language has changed to catalan")

}

function spanish() {

    $.getJSON("scripts/json/text_cas.json", function(cas) {
        console.log("funciona?")
        console.log(cas); 
        for (let i = 0; i < ids.length; i++) {
            document.getElementById(ids[i]).innerHTML = cas[ids[i]]
        }
    });

    console.log("Language has changed to spanish")

}

function english() {
    $.getJSON("scripts/json/text_en.json", function(en) {
        console.log(en); 
        for (let i = 0; i < ids.length; i++) {
            document.getElementById(ids[i]).innerHTML = en[ids[i]]
        }
    });

    console.log("Language has changed to english")

}

Json文件:

{
    "titleText":"Blah blah",
    "llenguatge":"Language",
    "runBut":"Guess",
    "trainBut":"Train",
    "resultText":"Result:",
    "atention":"Warning!",
    "warn":"Blah blah blah",
    "coll0but":"Color tools",
}

(编辑)这是HTML:

    <div class="Content">
                <div id="nn_container">
                    <div id="canvas" style="display: inline-block;"></div>
                    <div>
                        <span id="result" class="eqCont">
                            <div id="run" class="half"></div>
                            <div id="answer" class="half">
                                <div id="lastColor">
                                    <b id="resultText"></b>
                                    <span id="clar" class="reult"></span>
                                </div>
                            </div>
                        </span>
                    </div>

                </div>

                <div id="info">
                    <div id="Warning1" class="warning">
                        <strong id="atention"></strong> <p class="text" id="warn"></p>
                    </div>
                </div>
</div>

我应该如何解决?

0 个答案:

没有答案