Using Javascript to change website language

前端 未结 4 1402
走了就别回头了
走了就别回头了 2020-11-30 23:00

I\'m working on a GUI website that can use several languages. The original HTML-files I got to work with were totally static. So if translation was needed I had to parse thr

4条回答
  •  心在旅途
    2020-11-30 23:02

    You can use data attributes: the fact that "HTML5 attributes are not supported in IE6 and IE7" means that you don't get the getAttribute() method or the dataset property for retrieving/accessing them. But you can still retrieve them as explained in this post.

    var geoff = document.getElementById("geoff");
    alert(geoff.getAttribute("data-geoff"));
    

    Even better, you can use jQuery .data() to support previous versions of IE.

    Something along these lines should work:

    $("[data-translate]").each(function(){
        var key = $(this).data('translate');
        $(this).html(dictionary[key][current_lang] || "N/A");
    });
    

    Working example: https://jsfiddle.net/x93oLad8/4/

提交回复
热议问题