mirror of
https://github.com/zulip/zulip.git
synced 2025-11-01 20:44:04 +00:00
With this commit, we change how we deal with translation for strings. Previously we used to fetch the translations data after loading which created a lot of unpleasant race bugs. So we changed this to use the `translation_data` sent in `page_params` which is available at load time. The previous fetching can be useful if we want to change the string to the changed language without reloading the page but since we ask the user to reload the page after changing the default language so fetching after loading isn't useful for us and hence we can add resource only once. Ultimately, we can remove the i18next plugins too. We leave the logic for clearing local storage, patched to fully clear it. Fixes: #9087.
46 lines
1.2 KiB
JavaScript
46 lines
1.2 KiB
JavaScript
// commonjs code goes here
|
|
import i18next from 'i18next';
|
|
|
|
window.i18n = i18next;
|
|
|
|
i18next
|
|
.init({
|
|
lng: 'lang',
|
|
resources: {
|
|
lang: {
|
|
translation: page_params.translation_data,
|
|
},
|
|
},
|
|
nsSeparator: false,
|
|
keySeparator: false,
|
|
interpolation: {
|
|
prefix: "__",
|
|
suffix: "__",
|
|
},
|
|
returnEmptyString: false, // Empty string is not a valid translation.
|
|
});
|
|
|
|
i18next.ensure_i18n = function (callback) {
|
|
callback();
|
|
};
|
|
|
|
// garbage collect all old-style i18n translation maps in localStorage.
|
|
$(function () {
|
|
if (!localstorage.supported()) {
|
|
return;
|
|
}
|
|
|
|
// 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);
|
|
});
|
|
|
|
// remove cached translations of older versions.
|
|
translations.forEach(function (translation_key) {
|
|
localStorage.removeItem(translation_key);
|
|
});
|
|
return this;
|
|
});
|