2015-10-26 17:11:44 +01:00
|
|
|
// commonjs code goes here
|
2016-05-13 12:44:03 +02:00
|
|
|
|
2017-06-15 19:48:48 +02:00
|
|
|
import i18next from 'i18next';
|
|
|
|
import XHR from 'i18next-xhr-backend';
|
|
|
|
import LngDetector from 'i18next-browser-languagedetector';
|
|
|
|
import Cache from 'i18next-localstorage-cache';
|
2017-08-09 01:58:57 +02:00
|
|
|
import localstorage from './localstorage';
|
2016-07-05 13:05:51 +02:00
|
|
|
|
2017-06-15 19:48:48 +02:00
|
|
|
window.i18n = i18next;
|
2016-05-13 12:44:03 +02:00
|
|
|
|
2018-02-21 09:09:41 +01:00
|
|
|
// Add those keys in this list which are received from the backend
|
|
|
|
// and are translated by calling i18n.t function on variables. For example,
|
|
|
|
// i18n.t(receivedFromBackend);
|
|
|
|
var toBeTranslated = [ // eslint-disable-line no-unused-vars
|
|
|
|
// The Emoji type name for the "text" emojiset choice
|
|
|
|
i18n.t('Plain text'),
|
|
|
|
];
|
|
|
|
|
2017-10-17 10:09:41 +02:00
|
|
|
function loadPath(languages) {
|
|
|
|
var language = languages[0];
|
|
|
|
if (language.indexOf('-') >= 0) {
|
|
|
|
language = language.replace('-', '_'); // Change zh-Hans to zh_Hans.
|
|
|
|
}
|
|
|
|
|
|
|
|
return '/static/locale/' + language + '/translations.json';
|
|
|
|
}
|
|
|
|
|
2017-06-15 19:48:48 +02:00
|
|
|
var backendOptions = {
|
2017-10-17 10:09:41 +02:00
|
|
|
loadPath: loadPath,
|
2017-06-15 19:48:48 +02:00
|
|
|
};
|
|
|
|
var callbacks = [];
|
|
|
|
var initialized = false;
|
2016-05-13 12:44:03 +02:00
|
|
|
|
2017-06-15 19:48:48 +02:00
|
|
|
var detectionOptions = {
|
|
|
|
order: ['htmlTag'],
|
|
|
|
htmlTag: document.documentElement,
|
|
|
|
};
|
2016-07-05 13:05:51 +02:00
|
|
|
|
2017-06-15 19:48:48 +02:00
|
|
|
var cacheOptions = {
|
2017-07-14 06:37:56 +02:00
|
|
|
enabled: !page_params.development,
|
2017-06-15 19:48:48 +02:00
|
|
|
prefix: 'i18next:' + page_params.server_generation + ':',
|
2017-07-14 06:34:35 +02:00
|
|
|
expirationTime: 2*24*60*60*1000, // 2 days
|
2017-06-15 19:48:48 +02:00
|
|
|
};
|
2016-06-10 09:03:36 +02:00
|
|
|
|
2017-06-15 19:48:48 +02:00
|
|
|
i18next.use(XHR)
|
|
|
|
.use(LngDetector)
|
|
|
|
.use(Cache)
|
|
|
|
.init({
|
|
|
|
nsSeparator: false,
|
|
|
|
keySeparator: false,
|
|
|
|
interpolation: {
|
|
|
|
prefix: "__",
|
|
|
|
suffix: "__",
|
|
|
|
},
|
|
|
|
backend: backendOptions,
|
|
|
|
detection: detectionOptions,
|
|
|
|
cache: cacheOptions,
|
|
|
|
fallbackLng: 'en',
|
2017-10-04 10:09:24 +02:00
|
|
|
returnEmptyString: false, // Empty string is not a valid translation.
|
2017-06-15 19:48:48 +02:00
|
|
|
}, function () {
|
|
|
|
var i;
|
|
|
|
initialized = true;
|
|
|
|
for (i=0; i<callbacks.length; i += 1) {
|
|
|
|
callbacks[i]();
|
2016-06-10 09:03:36 +02:00
|
|
|
}
|
2017-06-15 19:48:48 +02:00
|
|
|
});
|
2016-06-10 09:03:36 +02:00
|
|
|
|
2017-06-15 19:48:48 +02:00
|
|
|
i18next.ensure_i18n = function (callback) {
|
|
|
|
if (initialized) {
|
|
|
|
callback();
|
|
|
|
} else {
|
|
|
|
callbacks.push(callback);
|
|
|
|
}
|
|
|
|
};
|
2017-04-10 20:12:30 +02:00
|
|
|
|
|
|
|
// garbage collect all old i18n translation maps in localStorage.
|
|
|
|
$(function () {
|
2017-07-28 21:18:33 +02:00
|
|
|
if (!localstorage.supported()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-10 20:12:30 +02:00
|
|
|
// this collects all localStorage keys that match the format of:
|
|
|
|
// i18next:dddddddddd:w+ => 1484902202:en
|
|
|
|
// these are all language translation strings.
|
|
|
|
var translations = Object.keys(localStorage).filter(function (key) {
|
|
|
|
return /^i18next:\d{10}:\w+$/.test(key);
|
|
|
|
});
|
|
|
|
|
2017-07-19 10:17:06 +02:00
|
|
|
var current_generation_key = 'i18next:' + page_params.server_generation;
|
|
|
|
// remove cached translations of older versions.
|
2017-04-10 20:12:30 +02:00
|
|
|
translations.forEach(function (translation_key) {
|
2017-08-22 16:25:03 +02:00
|
|
|
if (translation_key.indexOf(current_generation_key) !== 0) {
|
2017-07-19 10:17:06 +02:00
|
|
|
localStorage.removeItem(translation_key);
|
|
|
|
}
|
2017-04-10 20:12:30 +02:00
|
|
|
});
|
|
|
|
return this;
|
|
|
|
});
|