From e3f1d025aeee022e9edb83b94106fd716f77147a Mon Sep 17 00:00:00 2001 From: Brock Whittaker Date: Mon, 10 Apr 2017 11:12:30 -0700 Subject: [PATCH] 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. --- static/js/translations.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/static/js/translations.js b/static/js/translations.js index f77915f9e9..90a262d8d0 100644 --- a/static/js/translations.js +++ b/static/js/translations.js @@ -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; +});