简体   繁体   中英

Localize HTML-Page with JavaScript

I have a simple html-page, there I have several text-elements. This I want to localizise, english, german, and so.

Here I found some snippet, but I don't get them to work: Handling multilanguage with JQuery only Why is this not a correct syntax:

js:

label["login"]["fr"]="Connection";
label["login"]["en"]="Login";
...
//some Click-Listener change all language elements
$("*[tag='ist_ml']").each(function() {
    $(this).html(label[$(this).attr("ml_label")]["en"]);
});
//End Click-Listener

html:

<label tag='ist_ml' ml_label='login' for='txtLogin'></label>

Some help where great, or some other simple ideas.

Define the variables before trying to store data:

label = {};
label["login"] = {};
label["login"]["fr"]="Connection";
label["login"]["en"]="Login";

Or

label = {
    login : {
        fr : "Connection",
        en : "Login"
    }
};

I think what might be missing, or not be decleard:

var label = new Array();
label["login"] = new Array();
label["login"]["en"] = "Bla...";

your code has been refined and its working here

JS CODE:

var lb = {};
lb["login"] = {};

lb["login"]["fr"] = "Connection";
lb["login"]["en"] = "Login";

//some Click-Listener change all language elements
  $("label[class='is']").each(function () {
  $(this).html(lb[$(this).attr("ml_label")]["en"]);
});
//End Click-Listener

HTML CODE:

<label class='is' ml_label='login' for='txtLogin'></label>
<label class='is' ml_label='login' for='txtLogin'></label>

Here is the J SFIDDLE to play along with your code

Live,Dream,Cherish :)

Couple of minutes late to the party, but here's my working solution for reference:

http://jsfiddle.net/adnrw/jStET/1/

As mentioned above you need to declare the array() for label before you start adding things to it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM