From 4fc98720a8d751a9ddb430bb24d6ac435f6f9b19 Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Tue, 22 Jul 2014 21:53:16 +0200 Subject: [PATCH] [New] Initial commit --- .gitignore | 1 + en/messages.json | 5 +++ i18n.js | 98 ++++++++++++++++++++++++++++++++++++++++++++++++ i18n_test.html | 14 +++++++ 4 files changed, 118 insertions(+) create mode 100644 .gitignore create mode 100644 en/messages.json create mode 100644 i18n.js create mode 100644 i18n_test.html diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..485dee6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea diff --git a/en/messages.json b/en/messages.json new file mode 100644 index 0000000..9c8394e --- /dev/null +++ b/en/messages.json @@ -0,0 +1,5 @@ +{ + "app.name" : "My App", + "app.title" : "My title", + "myElement.i18n": "Lorem ipsum" +} diff --git a/i18n.js b/i18n.js new file mode 100644 index 0000000..38bec23 --- /dev/null +++ b/i18n.js @@ -0,0 +1,98 @@ +var i18n = { + resource: "messages", + locale: null, + bundle: null, + detectLocale: function () { + var locale; + locale = navigator.language; // (Netscape - Browser Localization) + if (!locale) { + navigator.browserLanguage; //(IE-Specific - Browser Localized Language) + } + if (!locale) { + navigator.systemLanguage; //(IE-Specific - Windows OS - Localized Language) + } + if (!locale) { + navigator.userLanguage; + } + if (!locale) { + locale = "en"; + console.info("Defaulting to " + locale) + } + + if (locale.length > 2) { + console.info("Shortening locale to top level: " + locale) + locale = locale.substring(0, 2); + } + + console.info("Current locale: " + locale); + return locale; + }, + + load18n: function (locale, resource) { + + var bundle = [null]; + + var client = new XMLHttpRequest(); + client.onreadystatechange = function () { + if (client.readyState == 4) { + if (client.status == 200) { + 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); + } + } + }; + + var url = locale + "/" + resource + ".json"; + client.open("GET", url, false); + client.setRequestHeader("Accept", "application/json"); + //client.setRequestHeader("Content-Type", "plain/text"); // "application/x-www-form-urlencoded" + client.send(); + + return bundle[0]; + }, + + t: function (key) { + if (this.locale == null) { + this.locale = this.detectLocale(); + } + if (this.bundle == null) { + this.bundle = this.load18n(this.locale, this.resource); + } + + var msg = this.bundle[key]; + if (msg == null) + msg = key; + + return msg; + }, + + translate_document: function () { + var listToI18n = document.querySelectorAll("*[data-i18n]"); + console.info("Translating " + listToI18n.length + " elements.") + Array.prototype.slice.call(listToI18n).forEach(function (itemToI18n, index, arr) { + var key = itemToI18n.getAttribute("data-i18n"); + if (key != null && key.length > 0) { + var msg = i18n.t(key); + if (msg != null || msg.length == 0) { + console.info("Missing translation for key: " + key); + } else { + itemToI18n.innerText = msg; + } + } + }); + } +} + +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/i18n_test.html b/i18n_test.html new file mode 100644 index 0000000..a20574f --- /dev/null +++ b/i18n_test.html @@ -0,0 +1,14 @@ + + + i18n test + + + + +

Bla bla

+ +

Original text...

+ +

Untranslated bla bla

+ + \ No newline at end of file