Try to auto-detect user language and translate web.

This commit is contained in:
onovy 2014-01-19 22:00:13 +01:00
parent b6fb655b3a
commit 1b9b84bd8a
3 changed files with 32 additions and 0 deletions

View file

@ -6895,6 +6895,17 @@ ninja.foreachSerialized = function (collection, whatToDo, onComplete) {
ninja.translator = { ninja.translator = {
currentCulture: "en", currentCulture: "en",
autodetectTranslation: function() {
// window.navigator.language for Firefox / Chrome / Opera Safari
// window.navigator.userLanguage for IE
var language = window.navigator.language || window.navigator.userLanguage;
if (!ninja.translator.translate(language)) {
// Try to remove part after dash, for example cs-CZ -> cs
language = language.substr(0, language.indexOf('-'));
ninja.translator.translate(language);
}
},
translate: function (culture) { translate: function (culture) {
var dict = ninja.translator.translations[culture]; var dict = ninja.translator.translations[culture];
if (dict) { if (dict) {
@ -6914,6 +6925,9 @@ ninja.translator = {
document.getElementById(id).innerHTML = dict[id]; document.getElementById(id).innerHTML = dict[id];
} }
} }
return true;
} else {
return false;
} }
}, },
@ -9084,6 +9098,8 @@ if (ninja.getQueryString()["asyncunittests"] == "true" || ninja.getQueryString()
// change language // change language
if (ninja.getQueryString()["culture"] != undefined) { if (ninja.getQueryString()["culture"] != undefined) {
ninja.translator.translate(ninja.getQueryString()["culture"]); ninja.translator.translate(ninja.getQueryString()["culture"]);
} else {
ninja.translator.autodetectTranslation();
} }
// testnet, check if testnet edition should be activated // testnet, check if testnet edition should be activated
if (ninja.getQueryString()["testnet"] == "true" || ninja.getQueryString()["testnet"] == "1") { if (ninja.getQueryString()["testnet"] == "true" || ninja.getQueryString()["testnet"] == "1") {

View file

@ -10,6 +10,8 @@ if (ninja.getQueryString()["asyncunittests"] == "true" || ninja.getQueryString()
// change language // change language
if (ninja.getQueryString()["culture"] != undefined) { if (ninja.getQueryString()["culture"] != undefined) {
ninja.translator.translate(ninja.getQueryString()["culture"]); ninja.translator.translate(ninja.getQueryString()["culture"]);
} else {
ninja.translator.autodetectTranslation();
} }
// testnet, check if testnet edition should be activated // testnet, check if testnet edition should be activated
if (ninja.getQueryString()["testnet"] == "true" || ninja.getQueryString()["testnet"] == "1") { if (ninja.getQueryString()["testnet"] == "true" || ninja.getQueryString()["testnet"] == "1") {

View file

@ -1,6 +1,17 @@
ninja.translator = { ninja.translator = {
currentCulture: "en", currentCulture: "en",
autodetectTranslation: function() {
// window.navigator.language for Firefox / Chrome / Opera Safari
// window.navigator.userLanguage for IE
var language = window.navigator.language || window.navigator.userLanguage;
if (!ninja.translator.translate(language)) {
// Try to remove part after dash, for example cs-CZ -> cs
language = language.substr(0, language.indexOf('-'));
ninja.translator.translate(language);
}
},
translate: function (culture) { translate: function (culture) {
var dict = ninja.translator.translations[culture]; var dict = ninja.translator.translations[culture];
if (dict) { if (dict) {
@ -20,6 +31,9 @@ ninja.translator = {
document.getElementById(id).innerHTML = dict[id]; document.getElementById(id).innerHTML = dict[id];
} }
} }
return true;
} else {
return false;
} }
}, },