Refactore layout and added README.md

This commit is contained in:
Robert von Burg 2014-07-22 22:14:11 +02:00
parent 4fc98720a8
commit c013ca11c9
4 changed files with 27 additions and 17 deletions

View File

@ -1,4 +1,15 @@
js_i18n
=======
Simple JavaScript library to internationalise html files
Usage
=======
Include in your HTML file:
<script src="i18n.js"></script>
Then initialize somehow after page load:
i18n.init();
i18n.translate_document();
Translate single keys:
var msg = i18n.t('my_key');

View File

@ -2,6 +2,7 @@ var i18n = {
resource: "messages",
locale: null,
bundle: null,
detectLocale: function () {
var locale;
locale = navigator.language; // (Netscape - Browser Localization)
@ -39,8 +40,7 @@ var i18n = {
var queryResult = JSON.parse(client.responseText);
bundle[0] = queryResult;
} else {
alert("The request did not succeed!\n\nThe response status was: " + client.status + " "
+ client.statusText + ".\n\nClient response: " + client.responseText);
alert("The request did not succeed!\n\nThe response status was: " + client.status + " " + client.statusText + ".\n\nClient response: " + client.responseText);
}
}
};
@ -54,13 +54,17 @@ var i18n = {
return bundle[0];
},
t: function (key) {
init: function () {
if (this.locale == null) {
this.locale = this.detectLocale();
}
if (this.bundle == null) {
this.bundle = this.load18n(this.locale, this.resource);
}
},
t: function (key) {
this.init();
var msg = this.bundle[key];
if (msg == null)
@ -76,7 +80,7 @@ var i18n = {
var key = itemToI18n.getAttribute("data-i18n");
if (key != null && key.length > 0) {
var msg = i18n.t(key);
if (msg != null || msg.length == 0) {
if (msg == null || msg.length == 0) {
console.info("Missing translation for key: " + key);
} else {
itemToI18n.innerText = msg;
@ -85,14 +89,3 @@ var i18n = {
});
}
}
function run() {
// translate all elements which have a "data-i18n" attribute
i18n.translate_document();
// translate a single item:
var myValue = i18n.t("app.name")
var elem = document.getElementById("myElement");
elem.innerText = myValue
}

View File

@ -2,7 +2,13 @@
<head>
<title>i18n test</title>
<script src="i18n.js"></script>
<script type="text/javascript" src="../lib/i18n.js"></script>
<script type="text/javascript">
var run = function() {
i18n.init();
i18n.translate_document();
}
</script>
</head>
<body onload="run()">
<h1>Bla bla</h1>