i18n: Garbage collect old translations from localStorage.

The old translation copies in localStorage were not being removed
when they were no longer needed, so we can free up the storage
by deleting them.

This was accidentally not merged months ago when originally
implemented, but it was written to fix #4443 and in fact does so.
This commit is contained in:
Brock Whittaker
2017-04-10 11:12:30 -07:00
committed by Tim Abbott
parent 59b5a2f862
commit e3f1d025ae

View File

@@ -53,3 +53,30 @@ i18next.ensure_i18n = function (callback) {
callbacks.push(callback);
}
};
// garbage collect all old i18n translation maps in localStorage.
$(function () {
// 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);
});
// by sorting them we get the lowest timestamps at the bottom and the
// most recent at the top.
translations = translations.sort();
// Remove the latest few translations (should include the
// currently in-use one for this and any recent tabs) from the
// list of items to delete.
translations.pop();
translations.pop();
translations.pop();
// remove all the old translations.
translations.forEach(function (translation_key) {
localStorage.removeItem(translation_key);
});
return this;
});