From c013ca11c95b057d2e6fb5c66fd67f5a083cbf87 Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Tue, 22 Jul 2014 22:14:11 +0200 Subject: [PATCH] Refactore layout and added README.md --- README.md | 13 ++++++++++++- i18n.js => lib/i18n.js | 23 ++++++++--------------- {en => test/en}/messages.json | 0 i18n_test.html => test/i18n_test.html | 8 +++++++- 4 files changed, 27 insertions(+), 17 deletions(-) rename i18n.js => lib/i18n.js (85%) rename {en => test/en}/messages.json (100%) rename i18n_test.html => test/i18n_test.html (50%) diff --git a/README.md b/README.md index 085a2cf..27f5711 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,15 @@ js_i18n ======= - Simple JavaScript library to internationalise html files + +Usage +======= +Include in your HTML file: + + +Then initialize somehow after page load: +i18n.init(); +i18n.translate_document(); + +Translate single keys: +var msg = i18n.t('my_key'); diff --git a/i18n.js b/lib/i18n.js similarity index 85% rename from i18n.js rename to lib/i18n.js index 38bec23..c221116 100644 --- a/i18n.js +++ b/lib/i18n.js @@ -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 -} diff --git a/en/messages.json b/test/en/messages.json similarity index 100% rename from en/messages.json rename to test/en/messages.json diff --git a/i18n_test.html b/test/i18n_test.html similarity index 50% rename from i18n_test.html rename to test/i18n_test.html index a20574f..12ac50d 100644 --- a/i18n_test.html +++ b/test/i18n_test.html @@ -2,7 +2,13 @@ i18n test - + +

Bla bla