[Major] Removed old i18n functions, add getUserLocale()
This commit is contained in:
parent
59c1344059
commit
b7a2d394df
304
Strolch.js
304
Strolch.js
|
@ -19,22 +19,6 @@ Strolch = {
|
||||||
activities: 'rest/strolch/model/activities'
|
activities: 'rest/strolch/model/activities'
|
||||||
},
|
},
|
||||||
|
|
||||||
translateI18n: function (locale) {
|
|
||||||
if (locale == null || locale.length == 0 || locale == 'undefined') {
|
|
||||||
console.log('Locale is \'' + locale + '\', detecting browser locale...');
|
|
||||||
locale = i18n.detectLocale();
|
|
||||||
console.log('Locale is now ' + locale);
|
|
||||||
}
|
|
||||||
|
|
||||||
this.i18n.path = 'localization';
|
|
||||||
this.i18n.locale = locale;
|
|
||||||
this.i18n.resource = 'localization';
|
|
||||||
this.i18n.init(locale);
|
|
||||||
this.i18n.translate_document();
|
|
||||||
|
|
||||||
this.setLocale(this.i18n.locale);
|
|
||||||
},
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* configuration
|
* configuration
|
||||||
*/
|
*/
|
||||||
|
@ -50,16 +34,14 @@ Strolch = {
|
||||||
getUserConfig: function () {
|
getUserConfig: function () {
|
||||||
if (this.props.userConfig == null) {
|
if (this.props.userConfig == null) {
|
||||||
var data = localStorage.userData;
|
var data = localStorage.userData;
|
||||||
if (data == null)
|
if (data == null) return null;
|
||||||
return null;
|
|
||||||
this.props.userConfig = JSON.parse(data);
|
this.props.userConfig = JSON.parse(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.props.userConfig;
|
return this.props.userConfig;
|
||||||
},
|
},
|
||||||
setUserConfig: function (data) {
|
setUserConfig: function (data) {
|
||||||
if (data == null)
|
if (data == null) throw "Data can not be null!";
|
||||||
throw "Data can not be null!";
|
|
||||||
this.props.userConfig = data;
|
this.props.userConfig = data;
|
||||||
return localStorage.userData = JSON.stringify(data);
|
return localStorage.userData = JSON.stringify(data);
|
||||||
},
|
},
|
||||||
|
@ -68,89 +50,69 @@ Strolch = {
|
||||||
delete localStorage.userData;
|
delete localStorage.userData;
|
||||||
},
|
},
|
||||||
|
|
||||||
getLocale: function () {
|
getUserLocale: function () {
|
||||||
|
if (localStorage.languageLocale) {
|
||||||
|
return localStorage.languageLocale;
|
||||||
|
}
|
||||||
|
|
||||||
|
var userConfig = this.getUserConfig();
|
||||||
|
var userLocale;
|
||||||
|
if (userConfig && this.isNotEmptyString(userConfig.locale)) {
|
||||||
|
userLocale = userConfig.locale;
|
||||||
|
if (userLocale.length > 2) {
|
||||||
|
userLocale = userLocale.substr(0, 2);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
var userLang;
|
||||||
|
if (navigator.languages && navigator.languages.length > 0) {
|
||||||
|
userLang = navigator.languages[0].substr(0, 2);
|
||||||
|
} else if (navigator.language) {
|
||||||
|
userLang = navigator.language.substr(0, 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (userLang == 'en' || userLang == 'de') {
|
||||||
|
userLocale = userLang;
|
||||||
|
} else {
|
||||||
|
userLocale = "en";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('Evaluated user locale as ' + userLocale);
|
||||||
|
|
||||||
|
localStorage.languageLocale = userLocale;
|
||||||
return localStorage.languageLocale;
|
return localStorage.languageLocale;
|
||||||
},
|
},
|
||||||
setLocale: function (locale) {
|
setUserLocale: function (locale) {
|
||||||
|
console.log('Setting locale to ' + locale);
|
||||||
localStorage.languageLocale = locale;
|
localStorage.languageLocale = locale;
|
||||||
},
|
},
|
||||||
|
|
||||||
version: function () {
|
|
||||||
var that = this;
|
|
||||||
|
|
||||||
if (this.props.version == null) {
|
|
||||||
this.props.version = "unknown";
|
|
||||||
$.ajax({
|
|
||||||
async: false,
|
|
||||||
url: that.urls.version
|
|
||||||
}).done(function (data) {
|
|
||||||
if (data != null) {
|
|
||||||
var ver = data.appVersion.artifactVersion;
|
|
||||||
var rev = data.appVersion.scmRevision;
|
|
||||||
if (rev == '${buildNumber}') {
|
|
||||||
that.props.version = ver;
|
|
||||||
} else {
|
|
||||||
ver = ver ? ver.substr(0, 9) : '?';
|
|
||||||
rev = rev ? rev.substr(0, 7) : '?';
|
|
||||||
that.props.version = ver + " - " + rev;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return this.props.version;
|
|
||||||
},
|
|
||||||
|
|
||||||
_revision: null,
|
|
||||||
revision: function () {
|
|
||||||
var that = this;
|
|
||||||
if (this.props.revision == null) {
|
|
||||||
this.props.revision = Math.floor(Math.random() * 10000000);
|
|
||||||
$.ajax({
|
|
||||||
async: false,
|
|
||||||
url: that.urls.version
|
|
||||||
}).done(function (data) {
|
|
||||||
if (data != null) {
|
|
||||||
var rev = data.appVersion.scmRevision;
|
|
||||||
if (rev != '${buildNumber}') {
|
|
||||||
that.props.revision = rev;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return this.props.revision;
|
|
||||||
},
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* session
|
* session
|
||||||
*/
|
*/
|
||||||
getUserProperty: function (name) {
|
getUserProperty: function (name) {
|
||||||
var userConfig = this.getUserConfig();
|
var userConfig = this.getUserConfig();
|
||||||
if (userConfig == null || userConfig.properties == null)
|
if (userConfig == null || userConfig.properties == null) return null;
|
||||||
return null;
|
|
||||||
var properties = userConfig.properties;
|
var properties = userConfig.properties;
|
||||||
return properties[name];
|
return properties[name];
|
||||||
},
|
},
|
||||||
getPrivilege: function (name) {
|
getPrivilege: function (name) {
|
||||||
var userConfig = this.getUserConfig();
|
var userConfig = this.getUserConfig();
|
||||||
if (userConfig == null)
|
if (userConfig == null) return null;
|
||||||
return null;
|
|
||||||
var privileges = userConfig.privileges;
|
var privileges = userConfig.privileges;
|
||||||
for (var i = 0; i < privileges.length; i++) {
|
for (var i = 0; i < privileges.length; i++) {
|
||||||
var privilege = privileges[i];
|
var privilege = privileges[i];
|
||||||
if (privilege.name == name)
|
if (privilege.name == name) return privilege;
|
||||||
return privilege;
|
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
},
|
},
|
||||||
hasRole: function (name) {
|
hasRole: function (name) {
|
||||||
var userConfig = this.getUserConfig();
|
var userConfig = this.getUserConfig();
|
||||||
if (userConfig == null)
|
if (userConfig == null) return null;
|
||||||
return null;
|
|
||||||
var roles = userConfig.roles;
|
var roles = userConfig.roles;
|
||||||
for (var i = 0; i < roles.length; i++) {
|
for (var i = 0; i < roles.length; i++) {
|
||||||
var role = roles[i];
|
var role = roles[i];
|
||||||
if (role == name)
|
if (role == name) return true;
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
},
|
},
|
||||||
|
@ -171,12 +133,10 @@ Strolch = {
|
||||||
}
|
}
|
||||||
|
|
||||||
// now we handle the privilege access
|
// now we handle the privilege access
|
||||||
if (privilege.allAllowed)
|
if (privilege.allAllowed) return true;
|
||||||
return true;
|
|
||||||
|
|
||||||
for (var i = 0; i < privilege.allowList.length; i++) {
|
for (var i = 0; i < privilege.allowList.length; i++) {
|
||||||
if (privilege.allowList[i] == privilegeValue)
|
if (privilege.allowList[i] == privilegeValue) return true;
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
@ -189,8 +149,7 @@ Strolch = {
|
||||||
getQueryParamsAsString: function () {
|
getQueryParamsAsString: function () {
|
||||||
var hash = document.location.hash;
|
var hash = document.location.hash;
|
||||||
var hashParts = hash.split('?');
|
var hashParts = hash.split('?');
|
||||||
if (hashParts.length !== 2)
|
if (hashParts.length !== 2) return '';
|
||||||
return '';
|
|
||||||
return hashParts[1];
|
return hashParts[1];
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -198,18 +157,16 @@ Strolch = {
|
||||||
|
|
||||||
var hash = document.location.hash;
|
var hash = document.location.hash;
|
||||||
var hashParts = hash.split('?');
|
var hashParts = hash.split('?');
|
||||||
if (hashParts.length !== 2)
|
if (hashParts.length !== 2) return null;
|
||||||
return null;
|
|
||||||
|
|
||||||
var queryParams = hashParts[1];
|
var queryParams = hashParts[1];
|
||||||
var queryArr = queryParams.split('&');
|
var queryArr = queryParams.split('&');
|
||||||
for (var i = 0; i < queryArr.length; i++) {
|
for (var i = 0; i < queryArr.length; i++) {
|
||||||
var queryParam = queryArr[i].split('=');
|
var queryParam = queryArr[i].split('=');
|
||||||
if (queryParam.length !== 2)
|
if (queryParam.length !== 2) continue;
|
||||||
continue;
|
|
||||||
var name = queryParam[0];
|
var name = queryParam[0];
|
||||||
var value = queryParam[1];
|
var value = queryParam[1];
|
||||||
if (name === paramName && Strolch.isNotEmptyString(value)) {
|
if (name === paramName && this.isNotEmptyString(value)) {
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -221,14 +178,14 @@ Strolch = {
|
||||||
var hash = document.location.hash;
|
var hash = document.location.hash;
|
||||||
var hashParts = hash.split('?');
|
var hashParts = hash.split('?');
|
||||||
if (hashParts.length !== 2) {
|
if (hashParts.length !== 2) {
|
||||||
if (Strolch.isNotEmptyString(paramValue)) {
|
if (this.isNotEmptyString(paramValue)) {
|
||||||
document.location.hash = hash + '/?' + paramName + '=' + paramValue;
|
document.location.hash = hash + '/?' + paramName + '=' + paramValue;
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Strolch.isEmptyString(hashParts[1])) {
|
if (this.isEmptyString(hashParts[1])) {
|
||||||
if (Strolch.isNotEmptyString(paramValue)) {
|
if (this.isNotEmptyString(paramValue)) {
|
||||||
document.location.hash = hashParts[0] + '?' + paramName + '=' + paramValue;
|
document.location.hash = hashParts[0] + '?' + paramName + '=' + paramValue;
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
|
@ -250,7 +207,7 @@ Strolch = {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Strolch.isNotEmptyString(paramValue)) {
|
if (this.isNotEmptyString(paramValue)) {
|
||||||
if (i != 0) {
|
if (i != 0) {
|
||||||
hash += '&';
|
hash += '&';
|
||||||
}
|
}
|
||||||
|
@ -259,7 +216,7 @@ Strolch = {
|
||||||
set = true;
|
set = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!set && Strolch.isNotEmptyString(paramValue)) {
|
if (!set && this.isNotEmptyString(paramValue)) {
|
||||||
if (i != 0) {
|
if (i != 0) {
|
||||||
hash += '&';
|
hash += '&';
|
||||||
}
|
}
|
||||||
|
@ -343,41 +300,33 @@ Strolch = {
|
||||||
},
|
},
|
||||||
|
|
||||||
toLocalDate: function (val) {
|
toLocalDate: function (val) {
|
||||||
if (this.isEmptyString(val) || val == '-')
|
if (this.isEmptyString(val) || val == '-') return '-';
|
||||||
return '-';
|
|
||||||
var date = new Date(val);
|
var date = new Date(val);
|
||||||
if (this.props.locale != null)
|
if (this.props.locale != null) return date.toLocaleDateString(this.props.locale);
|
||||||
return date.toLocaleDateString(this.props.locale);
|
|
||||||
return date.toLocaleDateString('de-CH')
|
return date.toLocaleDateString('de-CH')
|
||||||
},
|
},
|
||||||
|
|
||||||
toLocalDateTime: function (val) {
|
toLocalDateTime: function (val) {
|
||||||
if (this.isEmptyString(val) || val == '-')
|
if (this.isEmptyString(val) || val == '-') return '-';
|
||||||
return '-';
|
|
||||||
var date = new Date(val);
|
var date = new Date(val);
|
||||||
if (this.props.locale != null)
|
if (this.props.locale != null) return date.toLocaleDateString(this.props.locale) + ' ' + date.toLocaleTimeString(this.props.locale);
|
||||||
return date.toLocaleDateString(this.props.locale) + ' ' + date.toLocaleTimeString(this.props.locale);
|
|
||||||
return date.toLocaleDateString('de-CH') + ' ' + date.toLocaleTimeString('de-CH');
|
return date.toLocaleDateString('de-CH') + ' ' + date.toLocaleTimeString('de-CH');
|
||||||
},
|
},
|
||||||
|
|
||||||
toDateTime: function (val) {
|
toDateTime: function (val) {
|
||||||
|
|
||||||
function pad10(i) {
|
function pad10(i) {
|
||||||
if (i < 10)
|
if (i < 10) return '0' + i;
|
||||||
return '0' + i;
|
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
function pad100(i) {
|
function pad100(i) {
|
||||||
if (i < 10)
|
if (i < 10) return '00' + i;
|
||||||
return '00' + i;
|
if (i < 100) return '0' + i;
|
||||||
if (i < 100)
|
|
||||||
return '0' + i;
|
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.isEmptyString(val) || val == '-')
|
if (this.isEmptyString(val) || val == '-') return '-';
|
||||||
return '-';
|
|
||||||
var date = new Date(val);
|
var date = new Date(val);
|
||||||
|
|
||||||
var y = date.getFullYear();
|
var y = date.getFullYear();
|
||||||
|
@ -392,8 +341,7 @@ Strolch = {
|
||||||
},
|
},
|
||||||
|
|
||||||
isInfinite: function (val) {
|
isInfinite: function (val) {
|
||||||
if (val == '-')
|
if (val == '-') return true;
|
||||||
return true;
|
|
||||||
return moment(val).isAfter(moment('2100-01-01'));
|
return moment(val).isAfter(moment('2100-01-01'));
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -423,140 +371,10 @@ Strolch = {
|
||||||
|
|
||||||
weekdays: function () {
|
weekdays: function () {
|
||||||
var days = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'];
|
var days = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'];
|
||||||
return rollingSequence(days);
|
return this.rollingSequence(days);
|
||||||
},
|
},
|
||||||
daytime: function () {
|
daytime: function () {
|
||||||
var dayTimes = ['morning', 'midday', 'evening', 'night'];
|
var dayTimes = ['morning', 'midday', 'evening', 'night'];
|
||||||
return rollingSequence(dayTimes);
|
return this.rollingSequence(dayTimes);
|
||||||
},
|
|
||||||
|
|
||||||
|
|
||||||
i18n: {
|
|
||||||
resource: "messages",
|
|
||||||
path: null,
|
|
||||||
locale: null,
|
|
||||||
bundle: null,
|
|
||||||
|
|
||||||
detectLocale: function () {
|
|
||||||
var locale;
|
|
||||||
locale = navigator.language; // (Netscape - Browser Localization)
|
|
||||||
if (!locale) {
|
|
||||||
locale = navigator.browserLanguage; // (IE-Specific - Browser Localized Language)
|
|
||||||
}
|
|
||||||
if (!locale) {
|
|
||||||
locale = navigator.systemLanguage; // (IE-Specific - Windows OS - Localized Language)
|
|
||||||
}
|
|
||||||
if (!locale) {
|
|
||||||
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 (path, locale, resource) {
|
|
||||||
|
|
||||||
var jsonGet = function (url) {
|
|
||||||
var data = {result: {}};
|
|
||||||
|
|
||||||
var client = new XMLHttpRequest();
|
|
||||||
client.onreadystatechange = function () {
|
|
||||||
if (client.readyState == 4) {
|
|
||||||
if (client.status == 200) {
|
|
||||||
try {
|
|
||||||
var queryResult = JSON.parse(client.responseText);
|
|
||||||
data.result = queryResult;
|
|
||||||
} catch (e) {
|
|
||||||
alert("Failed to parse i18n from server: " + e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
client.open("GET", url, false);
|
|
||||||
client.setRequestHeader("Accept", "application/json");
|
|
||||||
client.send();
|
|
||||||
return data.result;
|
|
||||||
};
|
|
||||||
|
|
||||||
var locales = [];
|
|
||||||
if (locale != null) {
|
|
||||||
locales.push(locale);
|
|
||||||
var sepIndex = locale.indexOf("-", 0);
|
|
||||||
if (sepIndex == -1) sepIndex = locale.indexOf("_", 0);
|
|
||||||
if (sepIndex != -1) locales.push(locale.substring(0, sepIndex));
|
|
||||||
}
|
|
||||||
if (locale.indexOf("en") != 0) locales.push("en");
|
|
||||||
|
|
||||||
var bundle = null;
|
|
||||||
for (var i = 0; i < locales.length; ++i) {
|
|
||||||
var loc = locales[i];
|
|
||||||
var url = path + "/" + loc + "/" + resource + ".json?" + Math.random(); // prevent caching
|
|
||||||
bundle = jsonGet(url);
|
|
||||||
if (bundle != null) {
|
|
||||||
this.locale = loc;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return bundle;
|
|
||||||
},
|
|
||||||
|
|
||||||
init: function () {
|
|
||||||
if (this.locale == null) this.locale = this.detectLocale();
|
|
||||||
if (this.bundle == null) this.bundle = this.load18n(this.path, this.locale, this.resource);
|
|
||||||
},
|
|
||||||
|
|
||||||
t: function (key, properties) {
|
|
||||||
this.init();
|
|
||||||
var msg = this.bundle[key];
|
|
||||||
if (msg == null) msg = key;
|
|
||||||
|
|
||||||
if (properties) {
|
|
||||||
$.each(properties, function (key, value) {
|
|
||||||
msg = msg.replace('${' + key + '}', value);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return msg;
|
|
||||||
},
|
|
||||||
|
|
||||||
translate_document: function () {
|
|
||||||
|
|
||||||
var that = this;
|
|
||||||
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 = that.t(key);
|
|
||||||
if (msg == null || msg.length == 0) {
|
|
||||||
console.info("Missing translation for key: " + key);
|
|
||||||
} else {
|
|
||||||
if (itemToI18n.localName == 'input') {
|
|
||||||
if (itemToI18n.type == 'submit' || itemToI18n.type == 'button') {
|
|
||||||
itemToI18n.value = msg;
|
|
||||||
} else {
|
|
||||||
itemToI18n.placeholder = msg;
|
|
||||||
}
|
|
||||||
} else if (itemToI18n.localName == 'button') {
|
|
||||||
itemToI18n.text = msg;
|
|
||||||
} else {
|
|
||||||
itemToI18n.textContent = msg;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "strolchjs",
|
"name": "strolchjs",
|
||||||
"version": "0.1.4",
|
"version": "0.2.0",
|
||||||
"main": "strolch.js",
|
"main": "strolch.js",
|
||||||
"ignore": [
|
"ignore": [
|
||||||
"**/.*",
|
"**/.*",
|
||||||
|
|
Loading…
Reference in New Issue