2015-07-27 11:18:10 -07:00
|
|
|
|
(function () {
|
|
|
|
|
|
|
|
|
|
var dictionaries = {};
|
|
|
|
|
|
|
|
|
|
function getUrl(name, culture) {
|
|
|
|
|
|
2015-07-27 12:16:30 -07:00
|
|
|
|
var parts = culture.split('-');
|
|
|
|
|
if (parts.length == 2) {
|
|
|
|
|
parts[1] = parts[1].toUpperCase();
|
|
|
|
|
culture = parts.join('-');
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-27 11:18:10 -07:00
|
|
|
|
return 'strings/' + name + '/' + culture + '.json';
|
|
|
|
|
}
|
|
|
|
|
function getDictionary(name, culture) {
|
|
|
|
|
|
|
|
|
|
return dictionaries[getUrl(name, culture)];
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-28 14:48:52 -07:00
|
|
|
|
function loadDictionary(name, culture) {
|
2015-07-27 11:18:10 -07:00
|
|
|
|
|
2015-12-14 08:43:03 -07:00
|
|
|
|
return new Promise(function (resolve, reject) {
|
2015-07-27 11:18:10 -07:00
|
|
|
|
|
2015-12-14 08:43:03 -07:00
|
|
|
|
if (getDictionary(name, culture)) {
|
2016-01-19 18:10:46 -07:00
|
|
|
|
console.log('Globalize loadDictionary resolved: ' + name);
|
2015-12-14 08:43:03 -07:00
|
|
|
|
resolve();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2015-07-27 11:18:10 -07:00
|
|
|
|
|
2015-07-28 14:48:52 -07:00
|
|
|
|
var url = getUrl(name, culture);
|
|
|
|
|
|
2015-12-14 08:43:03 -07:00
|
|
|
|
var requestUrl = url + "?v=" + AppInfo.appVersion;
|
2015-07-28 14:48:52 -07:00
|
|
|
|
|
2015-12-23 10:46:01 -07:00
|
|
|
|
console.log('Requesting ' + requestUrl);
|
2015-07-28 14:48:52 -07:00
|
|
|
|
|
2015-12-14 08:43:03 -07:00
|
|
|
|
var xhr = new XMLHttpRequest();
|
|
|
|
|
xhr.open('GET', requestUrl, true);
|
2015-07-28 14:48:52 -07:00
|
|
|
|
|
2015-12-14 08:43:03 -07:00
|
|
|
|
var onError = function () {
|
2015-07-28 14:48:52 -07:00
|
|
|
|
|
2015-12-23 10:46:01 -07:00
|
|
|
|
console.log('Dictionary not found. Reverting to english');
|
2015-07-28 14:48:52 -07:00
|
|
|
|
|
2015-12-14 08:43:03 -07:00
|
|
|
|
// Grab the english version
|
|
|
|
|
var xhr2 = new XMLHttpRequest();
|
|
|
|
|
xhr2.open('GET', getUrl(name, 'en-US'), true);
|
|
|
|
|
|
|
|
|
|
xhr2.onload = function (e) {
|
|
|
|
|
dictionaries[url] = JSON.parse(this.response);
|
2016-01-19 18:10:46 -07:00
|
|
|
|
console.log('Globalize loadDictionary resolved: ' + name);
|
2015-12-14 08:43:03 -07:00
|
|
|
|
resolve();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
xhr2.send();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
xhr.onload = function (e) {
|
|
|
|
|
|
2015-12-23 10:46:01 -07:00
|
|
|
|
console.log('Globalize response status: ' + this.status);
|
2015-12-14 08:43:03 -07:00
|
|
|
|
|
|
|
|
|
if (this.status < 400) {
|
2015-07-27 11:18:10 -07:00
|
|
|
|
|
2015-12-14 08:43:03 -07:00
|
|
|
|
dictionaries[url] = JSON.parse(this.response);
|
2016-01-19 18:10:46 -07:00
|
|
|
|
console.log('Globalize loadDictionary resolved: ' + name);
|
2015-12-14 08:43:03 -07:00
|
|
|
|
resolve();
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
onError();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
xhr.onerror = onError;
|
|
|
|
|
|
|
|
|
|
xhr.send();
|
|
|
|
|
});
|
2015-07-27 11:18:10 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var currentCulture = 'en-US';
|
|
|
|
|
function setCulture(value) {
|
|
|
|
|
|
2015-12-23 10:46:01 -07:00
|
|
|
|
console.log('Setting culture to ' + value);
|
2015-07-27 11:18:10 -07:00
|
|
|
|
currentCulture = value;
|
|
|
|
|
|
2015-12-14 08:43:03 -07:00
|
|
|
|
return Promise.all([loadDictionary('html', value), loadDictionary('javascript', value)]);
|
2015-07-28 14:48:52 -07:00
|
|
|
|
}
|
|
|
|
|
|
2015-08-05 18:21:18 -07:00
|
|
|
|
function normalizeLocaleName(culture) {
|
|
|
|
|
|
|
|
|
|
culture = culture.replace('_', '-');
|
|
|
|
|
|
|
|
|
|
// If it's de-DE, convert to just de
|
|
|
|
|
var parts = culture.split('-');
|
|
|
|
|
if (parts.length == 2) {
|
|
|
|
|
if (parts[0].toLowerCase() == parts[1].toLowerCase()) {
|
|
|
|
|
culture = parts[0].toLowerCase();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return culture;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-28 14:48:52 -07:00
|
|
|
|
function getDeviceCulture() {
|
|
|
|
|
|
2015-12-14 08:43:03 -07:00
|
|
|
|
return new Promise(function (resolve, reject) {
|
2015-07-28 14:48:52 -07:00
|
|
|
|
|
2015-12-14 08:43:03 -07:00
|
|
|
|
if (AppInfo.isNativeApp) {
|
2015-08-05 18:21:18 -07:00
|
|
|
|
|
2015-12-14 08:43:03 -07:00
|
|
|
|
resolve(navigator.language || navigator.userLanguage);
|
2015-07-28 14:48:52 -07:00
|
|
|
|
|
2015-12-14 08:43:03 -07:00
|
|
|
|
} else if (AppInfo.supportsUserDisplayLanguageSetting) {
|
2015-07-28 14:48:52 -07:00
|
|
|
|
|
2015-12-23 10:46:01 -07:00
|
|
|
|
console.log('AppInfo.supportsUserDisplayLanguageSetting is true');
|
2015-08-02 16:47:31 -07:00
|
|
|
|
|
2015-12-14 08:43:03 -07:00
|
|
|
|
resolve(AppSettings.displayLanguage());
|
2015-08-02 16:47:31 -07:00
|
|
|
|
|
2015-12-14 08:43:03 -07:00
|
|
|
|
} else {
|
2015-07-28 12:42:24 -07:00
|
|
|
|
|
2015-12-23 10:46:01 -07:00
|
|
|
|
console.log('Getting culture from document');
|
2015-12-14 08:43:03 -07:00
|
|
|
|
resolve(document.documentElement.getAttribute('data-culture'));
|
|
|
|
|
}
|
|
|
|
|
});
|
2015-07-27 11:18:10 -07:00
|
|
|
|
}
|
|
|
|
|
|
2015-07-28 14:48:52 -07:00
|
|
|
|
|
2015-07-27 11:18:10 -07:00
|
|
|
|
function ensure() {
|
|
|
|
|
|
2015-12-23 10:46:01 -07:00
|
|
|
|
console.log('Entering Globalize.ensure');
|
2015-08-05 18:21:18 -07:00
|
|
|
|
|
2015-12-14 08:43:03 -07:00
|
|
|
|
return getDeviceCulture().then(function (culture) {
|
2015-07-27 11:18:10 -07:00
|
|
|
|
|
2015-12-14 08:43:03 -07:00
|
|
|
|
culture = normalizeLocaleName(culture || 'en-US');
|
2015-07-28 14:48:52 -07:00
|
|
|
|
|
2015-12-14 08:43:03 -07:00
|
|
|
|
return setCulture(culture);
|
2015-07-28 14:48:52 -07:00
|
|
|
|
});
|
2015-07-27 11:18:10 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function translateDocument(html, dictionaryName) {
|
|
|
|
|
|
2015-08-21 19:59:10 -07:00
|
|
|
|
dictionaryName = dictionaryName || 'html';
|
|
|
|
|
|
2015-07-27 11:18:10 -07:00
|
|
|
|
var glossary = getDictionary(dictionaryName, currentCulture) || {};
|
|
|
|
|
return translateHtml(html, glossary);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function translateHtml(html, dictionary) {
|
|
|
|
|
|
|
|
|
|
var startIndex = html.indexOf('${');
|
|
|
|
|
|
|
|
|
|
if (startIndex == -1) {
|
|
|
|
|
return html;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
startIndex += 2;
|
|
|
|
|
var endIndex = html.indexOf('}', startIndex);
|
|
|
|
|
|
|
|
|
|
if (endIndex == -1) {
|
|
|
|
|
return html;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var key = html.substring(startIndex, endIndex);
|
|
|
|
|
var val = dictionary[key] || key;
|
|
|
|
|
|
|
|
|
|
html = html.replace('${' + key + '}', val);
|
|
|
|
|
|
|
|
|
|
return translateHtml(html, dictionary);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Mimic Globalize api
|
|
|
|
|
// https://github.com/jquery/globalize
|
|
|
|
|
// Maybe later switch to it
|
|
|
|
|
|
|
|
|
|
window.Globalize = {
|
|
|
|
|
translate: function (key) {
|
|
|
|
|
|
|
|
|
|
var glossary = getDictionary('javascript', currentCulture) || {};
|
|
|
|
|
var val = glossary[key] || key;
|
|
|
|
|
|
|
|
|
|
for (var i = 1; i < arguments.length; i++) {
|
|
|
|
|
|
|
|
|
|
val = val.replace('{' + (i - 1) + '}', arguments[i]);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return val;
|
|
|
|
|
},
|
|
|
|
|
ensure: ensure,
|
|
|
|
|
translateDocument: translateDocument
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
})();
|