[Major] Removed old i18n functions, add getUserLocale()

This commit is contained in:
Robert von Burg 2017-09-12 14:56:33 +02:00
parent 59c1344059
commit b7a2d394df
2 changed files with 62 additions and 244 deletions

View File

@ -19,22 +19,6 @@ Strolch = {
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
*/
@ -50,16 +34,14 @@ Strolch = {
getUserConfig: function () {
if (this.props.userConfig == null) {
var data = localStorage.userData;
if (data == null)
return null;
if (data == null) return null;
this.props.userConfig = JSON.parse(data);
}
return this.props.userConfig;
},
setUserConfig: function (data) {
if (data == null)
throw "Data can not be null!";
if (data == null) throw "Data can not be null!";
this.props.userConfig = data;
return localStorage.userData = JSON.stringify(data);
},
@ -68,89 +50,69 @@ Strolch = {
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;
},
setLocale: function (locale) {
setUserLocale: function (locale) {
console.log('Setting locale to ' + 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
*/
getUserProperty: function (name) {
var userConfig = this.getUserConfig();
if (userConfig == null || userConfig.properties == null)
return null;
if (userConfig == null || userConfig.properties == null) return null;
var properties = userConfig.properties;
return properties[name];
},
getPrivilege: function (name) {
var userConfig = this.getUserConfig();
if (userConfig == null)
return null;
if (userConfig == null) return null;
var privileges = userConfig.privileges;
for (var i = 0; i < privileges.length; i++) {
var privilege = privileges[i];
if (privilege.name == name)
return privilege;
if (privilege.name == name) return privilege;
}
return null;
},
hasRole: function (name) {
var userConfig = this.getUserConfig();
if (userConfig == null)
return null;
if (userConfig == null) return null;
var roles = userConfig.roles;
for (var i = 0; i < roles.length; i++) {
var role = roles[i];
if (role == name)
return true;
if (role == name) return true;
}
return false;
},
@ -171,12 +133,10 @@ Strolch = {
}
// now we handle the privilege access
if (privilege.allAllowed)
return true;
if (privilege.allAllowed) return true;
for (var i = 0; i < privilege.allowList.length; i++) {
if (privilege.allowList[i] == privilegeValue)
return true;
if (privilege.allowList[i] == privilegeValue) return true;
}
return false;
@ -189,8 +149,7 @@ Strolch = {
getQueryParamsAsString: function () {
var hash = document.location.hash;
var hashParts = hash.split('?');
if (hashParts.length !== 2)
return '';
if (hashParts.length !== 2) return '';
return hashParts[1];
},
@ -198,18 +157,16 @@ Strolch = {
var hash = document.location.hash;
var hashParts = hash.split('?');
if (hashParts.length !== 2)
return null;
if (hashParts.length !== 2) return null;
var queryParams = hashParts[1];
var queryArr = queryParams.split('&');
for (var i = 0; i < queryArr.length; i++) {
var queryParam = queryArr[i].split('=');
if (queryParam.length !== 2)
continue;
if (queryParam.length !== 2) continue;
var name = queryParam[0];
var value = queryParam[1];
if (name === paramName && Strolch.isNotEmptyString(value)) {
if (name === paramName && this.isNotEmptyString(value)) {
return value;
}
}
@ -221,14 +178,14 @@ Strolch = {
var hash = document.location.hash;
var hashParts = hash.split('?');
if (hashParts.length !== 2) {
if (Strolch.isNotEmptyString(paramValue)) {
if (this.isNotEmptyString(paramValue)) {
document.location.hash = hash + '/?' + paramName + '=' + paramValue;
}
return;
}
if (Strolch.isEmptyString(hashParts[1])) {
if (Strolch.isNotEmptyString(paramValue)) {
if (this.isEmptyString(hashParts[1])) {
if (this.isNotEmptyString(paramValue)) {
document.location.hash = hashParts[0] + '?' + paramName + '=' + paramValue;
}
return;
@ -250,7 +207,7 @@ Strolch = {
continue;
}
if (Strolch.isNotEmptyString(paramValue)) {
if (this.isNotEmptyString(paramValue)) {
if (i != 0) {
hash += '&';
}
@ -259,7 +216,7 @@ Strolch = {
set = true;
}
if (!set && Strolch.isNotEmptyString(paramValue)) {
if (!set && this.isNotEmptyString(paramValue)) {
if (i != 0) {
hash += '&';
}
@ -343,41 +300,33 @@ Strolch = {
},
toLocalDate: function (val) {
if (this.isEmptyString(val) || val == '-')
return '-';
if (this.isEmptyString(val) || val == '-') return '-';
var date = new Date(val);
if (this.props.locale != null)
return date.toLocaleDateString(this.props.locale);
if (this.props.locale != null) return date.toLocaleDateString(this.props.locale);
return date.toLocaleDateString('de-CH')
},
toLocalDateTime: function (val) {
if (this.isEmptyString(val) || val == '-')
return '-';
if (this.isEmptyString(val) || val == '-') return '-';
var date = new Date(val);
if (this.props.locale != null)
return date.toLocaleDateString(this.props.locale) + ' ' + date.toLocaleTimeString(this.props.locale);
if (this.props.locale != null) return date.toLocaleDateString(this.props.locale) + ' ' + date.toLocaleTimeString(this.props.locale);
return date.toLocaleDateString('de-CH') + ' ' + date.toLocaleTimeString('de-CH');
},
toDateTime: function (val) {
function pad10(i) {
if (i < 10)
return '0' + i;
if (i < 10) return '0' + i;
return i;
}
function pad100(i) {
if (i < 10)
return '00' + i;
if (i < 100)
return '0' + i;
if (i < 10) return '00' + i;
if (i < 100) return '0' + i;
return i;
}
if (this.isEmptyString(val) || val == '-')
return '-';
if (this.isEmptyString(val) || val == '-') return '-';
var date = new Date(val);
var y = date.getFullYear();
@ -392,8 +341,7 @@ Strolch = {
},
isInfinite: function (val) {
if (val == '-')
return true;
if (val == '-') return true;
return moment(val).isAfter(moment('2100-01-01'));
},
@ -423,140 +371,10 @@ Strolch = {
weekdays: function () {
var days = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'];
return rollingSequence(days);
return this.rollingSequence(days);
},
daytime: function () {
var dayTimes = ['morning', 'midday', 'evening', 'night'];
return 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;
}
}
}
});
}
return this.rollingSequence(dayTimes);
}
};

View File

@ -1,6 +1,6 @@
{
"name": "strolchjs",
"version": "0.1.4",
"version": "0.2.0",
"main": "strolch.js",
"ignore": [
"**/.*",