From 08fb61ce36384ff90a704faffe4761eccfb76da1 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Mon, 23 Nov 2020 21:54:14 -0800 Subject: [PATCH] cache translations in client --- package.json | 2 ++ shared/translations/i18n.js | 24 +++++++++++++++++++----- yarn.lock | 16 +++++++++++++++- 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index ffc94c6a93..004cdf0aa8 100644 --- a/package.json +++ b/package.json @@ -94,7 +94,9 @@ "google-auth-library": "^5.5.1", "http-errors": "1.4.0", "i18next": "^19.8.3", + "i18next-chained-backend": "^2.0.1", "i18next-http-backend": "^1.0.21", + "i18next-localstorage-backend": "^3.1.2", "immutable": "^3.8.2", "imports-loader": "0.6.5", "invariant": "^2.2.2", diff --git a/shared/translations/i18n.js b/shared/translations/i18n.js index b77f86e17a..40207c4a0b 100644 --- a/shared/translations/i18n.js +++ b/shared/translations/i18n.js @@ -1,6 +1,8 @@ // @flow import i18n from "i18next"; -import backend from "i18next-http-backend"; +import Backend from "i18next-chained-backend"; +import HttpApi from "i18next-http-backend"; +import LocalStorageBackend from "i18next-localstorage-backend"; import { initReactI18next } from "react-i18next"; const initI18n = () => { @@ -8,12 +10,24 @@ const initI18n = () => { "DEFAULT_LANGUAGE" in process.env ? process.env.DEFAULT_LANGUAGE : "en_US"; i18n - .use(backend) + .use(Backend) .use(initReactI18next) .init({ - backend: { - loadPath: "/locales/{{lng}}.json", - }, + backend: + process.env.NODE_ENV === "test" + ? {} + : { + backends: [LocalStorageBackend, HttpApi], + backendOptions: [ + { + prefix: "translations-", + expirationTime: 7 * 24 * 60 * 60 * 1000, + }, + { + loadPath: "/locales/{{lng}}.json", + }, + ], + }, interpolation: { escapeValue: false, }, diff --git a/yarn.lock b/yarn.lock index 729461fdbd..ffbacd4c67 100644 --- a/yarn.lock +++ b/yarn.lock @@ -955,7 +955,7 @@ core-js-pure "^3.0.0" regenerator-runtime "^0.13.4" -"@babel/runtime@^7.0.0", "@babel/runtime@^7.1.2", "@babel/runtime@^7.10.2", "@babel/runtime@^7.11.2", "@babel/runtime@^7.12.0", "@babel/runtime@^7.12.1", "@babel/runtime@^7.3.1", "@babel/runtime@^7.8.4", "@babel/runtime@^7.9.2": +"@babel/runtime@^7.0.0", "@babel/runtime@^7.1.2", "@babel/runtime@^7.10.2", "@babel/runtime@^7.11.2", "@babel/runtime@^7.12.0", "@babel/runtime@^7.12.1", "@babel/runtime@^7.3.1", "@babel/runtime@^7.4.5", "@babel/runtime@^7.8.4", "@babel/runtime@^7.9.2": version "7.12.5" resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.12.5.tgz#410e7e487441e1b360c29be715d870d9b985882e" integrity sha512-plcc+hbExy3McchJCEQG3knOsuh3HH+Prx1P6cLIkET/0dLuQDEnrT+s27Axgc9bqfsmNUNHfscgMUdBpC9xfg== @@ -5926,6 +5926,13 @@ humanize-number@0.0.2: resolved "https://registry.yarnpkg.com/humanize-number/-/humanize-number-0.0.2.tgz#11c0af6a471643633588588048f1799541489c18" integrity sha1-EcCvakcWQ2M1iFiASPF5lUFInBg= +i18next-chained-backend@^2.0.1: + version "2.0.1" + resolved "https://registry.yarnpkg.com/i18next-chained-backend/-/i18next-chained-backend-2.0.1.tgz#762f78b30aafc5cab9f85f520f8281172380d1a1" + integrity sha512-IKLRCJ767tPWd3nKQMbcqbjd/tHcv+DjFIkaNd1yJSdnS6PME4lvCRYx+gl3gMMMbCL0r+l1bvRVBkTkRRXGVQ== + dependencies: + "@babel/runtime" "^7.4.5" + i18next-http-backend@^1.0.21: version "1.0.21" resolved "https://registry.yarnpkg.com/i18next-http-backend/-/i18next-http-backend-1.0.21.tgz#cee901b3527dad5165fa91de973b6aa6404c1c37" @@ -5933,6 +5940,13 @@ i18next-http-backend@^1.0.21: dependencies: node-fetch "2.6.1" +i18next-localstorage-backend@^3.1.2: + version "3.1.2" + resolved "https://registry.yarnpkg.com/i18next-localstorage-backend/-/i18next-localstorage-backend-3.1.2.tgz#415fcac42d6d66ff8bc878d269c5b5719fdedc2c" + integrity sha512-J81qf9atTAc9iMdHUNKJeAWgUsFvqbxfHIIztmF62tOaINA2AJk0kewJjm8LQDuArkiuW9RhQPbhqbyeZut35g== + dependencies: + "@babel/runtime" "^7.4.5" + i18next-parser@^3.3.0: version "3.3.0" resolved "https://registry.yarnpkg.com/i18next-parser/-/i18next-parser-3.3.0.tgz#5cd907aff5e791c043137a4902ab42631cde9656"