多语言丢失文本样式

Multi Language loosing text style

本文关键字:文本 样式 语言      更新时间:2023-09-26

我正在使用此代码开发一个多语言网站,我让它完美运行,但是当它发生变化时我遇到了问题,文本中的所有样式都丢失了,顶部菜单不仅失去了它的风格,而且还完全停止工作

这是更改文本的 javascript 函数:

(function() {
$.MultiLanguage = function(a, c) {
    var b = this;
    if (c == null) {
        c = null
    }
    return $.getJSON(a, function(g) {
        var f, d, h, e;
        if (c !== null) {
            localStorage.MultiLanguage = c
        } else {
            if (typeof localStorage.MultiLanguage === "undefined") {
                c = localStorage.MultiLanguage = g.config["default"]
            } else {
                c = localStorage.MultiLanguage
            }
        }
        d = g.language[c];
        e = [];
        for (f in d) {
            h = d[f];
            if ($(f).get(0).tagName.toLowerCase() === "title") {
                document.title = h;
                continue
            }
            if (f.length > 0) {
                e.push($(f).html(d[f]))
            } else {
                e.push(void 0)
            }
        }
        return e
    })
}
})
.call(this);

JSON 语言文件如下所示:

{
"config": {
    "default": "es"
},
"language": {
    "es": {         
        "#ml-home": "HOME",
        "#ml-agencia": "AGENCIA",
        "ml-img-serv":"images/labels/servicios.png",
        "#ml-texto_agencia": "Somos una agencia creativa y multidisciplinaria que busca potenciar a sus clientes. "
    },
    "en": {
        "#ml-home": "HOME",
        "#ml-agencia": "AGENCY",
        "ml-img-serv":"images/labels/agencia.png",      
        "#ml-texto_agencia": "Octagon is a creative and multidisciplinary agency seeking to empower its customers. "
    }
}
}

我发现了为什么它丢失了样式,该函数会替换 HTML 中的所有内容,如果 ID 设置不正确,甚至是类的样式。我修复了它,它工作正常。