2015-07-28 17:49:58 +00:00
|
|
|
/* global $, require, config, interfaceConfig */
|
2015-02-06 15:46:50 +00:00
|
|
|
var i18n = require("i18next-client");
|
|
|
|
var languages = require("../../service/translation/languages");
|
2015-02-24 10:49:46 +00:00
|
|
|
var Settings = require("../settings/Settings");
|
2015-02-06 15:46:50 +00:00
|
|
|
var DEFAULT_LANG = languages.EN;
|
|
|
|
|
|
|
|
i18n.addPostProcessor("resolveAppName", function(value, key, options) {
|
|
|
|
return value.replace("__app__", interfaceConfig.APP_NAME);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var defaultOptions = {
|
|
|
|
detectLngQS: "lang",
|
|
|
|
useCookie: false,
|
|
|
|
fallbackLng: DEFAULT_LANG,
|
|
|
|
load: "unspecific",
|
|
|
|
resGetPath: 'lang/__ns__-__lng__.json',
|
|
|
|
ns: {
|
|
|
|
namespaces: ['main', 'languages'],
|
|
|
|
defaultNs: 'main'
|
|
|
|
},
|
|
|
|
lngWhitelist : languages.getLanguages(),
|
|
|
|
fallbackOnNull: true,
|
2015-02-24 10:49:46 +00:00
|
|
|
fallbackOnEmpty: true,
|
2015-02-06 15:46:50 +00:00
|
|
|
useDataAttrOptions: true,
|
|
|
|
app: interfaceConfig.APP_NAME,
|
2015-02-26 15:35:35 +00:00
|
|
|
getAsync: false,
|
2015-02-27 17:58:05 +00:00
|
|
|
defaultValueFromContent: false,
|
2015-02-06 15:46:50 +00:00
|
|
|
customLoad: function(lng, ns, options, done) {
|
|
|
|
var resPath = "lang/__ns__-__lng__.json";
|
|
|
|
if(lng === languages.EN)
|
|
|
|
resPath = "lang/__ns__.json";
|
|
|
|
var url = i18n.functions.applyReplacement(resPath, { lng: lng, ns: ns });
|
|
|
|
i18n.functions.ajax({
|
|
|
|
url: url,
|
|
|
|
success: function(data, status, xhr) {
|
|
|
|
i18n.functions.log('loaded: ' + url);
|
|
|
|
done(null, data);
|
|
|
|
},
|
|
|
|
error : function(xhr, status, error) {
|
|
|
|
if ((status && status == 200) ||
|
|
|
|
(xhr && xhr.status && xhr.status == 200)) {
|
|
|
|
// file loaded but invalid json, stop waste time !
|
|
|
|
i18n.functions.error('There is a typo in: ' + url);
|
|
|
|
} else if ((status && status == 404) ||
|
|
|
|
(xhr && xhr.status && xhr.status == 404)) {
|
|
|
|
i18n.functions.log('Does not exist: ' + url);
|
|
|
|
} else {
|
|
|
|
var theStatus = status ? status :
|
|
|
|
((xhr && xhr.status) ? xhr.status : null);
|
|
|
|
i18n.functions.log(theStatus + ' when loading ' + url);
|
|
|
|
}
|
|
|
|
|
|
|
|
done(error, {});
|
|
|
|
},
|
|
|
|
dataType: "json",
|
|
|
|
async : options.getAsync
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// options for caching
|
|
|
|
// useLocalStorage: true,
|
|
|
|
// localStorageExpirationTime: 86400000 // in ms, default 1 week
|
|
|
|
};
|
|
|
|
|
2015-07-28 17:49:58 +00:00
|
|
|
function initCompleted(t) {
|
2015-02-06 15:46:50 +00:00
|
|
|
$("[data-i18n]").i18n();
|
|
|
|
}
|
|
|
|
|
2015-02-24 10:49:46 +00:00
|
|
|
function checkForParameter() {
|
|
|
|
var query = window.location.search.substring(1);
|
|
|
|
var vars = query.split("&");
|
|
|
|
for (var i=0;i<vars.length;i++) {
|
|
|
|
var pair = vars[i].split("=");
|
|
|
|
if(pair[0] == "lang")
|
|
|
|
{
|
|
|
|
return pair[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-02-06 15:46:50 +00:00
|
|
|
module.exports = {
|
|
|
|
init: function (lang) {
|
|
|
|
var options = defaultOptions;
|
2015-02-24 10:49:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
if(!lang)
|
|
|
|
{
|
|
|
|
lang = checkForParameter();
|
|
|
|
if(!lang)
|
|
|
|
{
|
|
|
|
var settings = Settings.getSettings();
|
|
|
|
if(settings)
|
|
|
|
lang = settings.language;
|
2015-05-29 14:09:44 +00:00
|
|
|
|
|
|
|
if(!lang && config.defaultLanguage)
|
|
|
|
{
|
|
|
|
lang = config.defaultLanguage;
|
|
|
|
}
|
2015-02-24 10:49:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(lang) {
|
2015-02-06 15:46:50 +00:00
|
|
|
options.lng = lang;
|
2015-02-24 10:49:46 +00:00
|
|
|
}
|
|
|
|
|
2015-02-06 15:46:50 +00:00
|
|
|
i18n.init(options, initCompleted);
|
|
|
|
},
|
2015-02-26 15:35:35 +00:00
|
|
|
translateString: function (key, options) {
|
|
|
|
return i18n.t(key, options);
|
2015-02-06 15:46:50 +00:00
|
|
|
},
|
|
|
|
setLanguage: function (lang) {
|
|
|
|
if(!lang)
|
|
|
|
lang = DEFAULT_LANG;
|
|
|
|
i18n.setLng(lang, defaultOptions, initCompleted);
|
|
|
|
},
|
|
|
|
getCurrentLanguage: function () {
|
|
|
|
return i18n.lng();
|
|
|
|
},
|
|
|
|
translateElement: function (selector) {
|
|
|
|
selector.i18n();
|
2015-02-20 16:17:46 +00:00
|
|
|
},
|
2015-07-03 09:34:05 +00:00
|
|
|
generateTranslationHTML: function (key, options) {
|
2015-02-20 16:17:46 +00:00
|
|
|
var str = "<span data-i18n=\"" + key + "\"";
|
|
|
|
if(options)
|
|
|
|
{
|
|
|
|
str += " data-i18n-options=\"" + JSON.stringify(options) + "\"";
|
|
|
|
}
|
|
|
|
str += ">";
|
2015-02-26 15:35:35 +00:00
|
|
|
str += this.translateString(key, options);
|
2015-02-20 16:17:46 +00:00
|
|
|
str += "</span>";
|
|
|
|
return str;
|
|
|
|
|
2015-02-06 15:46:50 +00:00
|
|
|
}
|
|
|
|
};
|