From c509890d2575655cc37338667d455946b5561f8e Mon Sep 17 00:00:00 2001 From: Thomas Ghysels Date: Fri, 26 Aug 2022 14:44:09 +0200 Subject: [PATCH 1/3] feat: add option to clear cache in @bothrs/translations --- packages/translations/src/index.ts | 3 ++- packages/translations/src/types.ts | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/translations/src/index.ts b/packages/translations/src/index.ts index d5a89f8..bf2b9a6 100644 --- a/packages/translations/src/index.ts +++ b/packages/translations/src/index.ts @@ -42,6 +42,7 @@ export function useTranslations( } export function initTranslations({ + storagePrefix = 'i18n_', expirationTime, fetchOptions, ...options @@ -60,7 +61,7 @@ export function initTranslations({ backend: { backends: [StorageBackend, MultiloadAdapter], backendOptions: [ - { prefix: 'i18n_', expirationTime }, + { prefix: storagePrefix, expirationTime }, { backend: Fetch, backendOption: fetchOptions }, ], }, diff --git a/packages/translations/src/types.ts b/packages/translations/src/types.ts index ba2defb..eb9fc1e 100644 --- a/packages/translations/src/types.ts +++ b/packages/translations/src/types.ts @@ -37,6 +37,8 @@ export interface TranslationRow { } export interface TranslationInitParameters extends InitOptions { + /** allows to bust the cache in a declarative way, warning: previous cached versions are kept in storage forever */ + storagePrefix?: string /** expirationTime time between between revalidation intervals, defaults to 1 week */ expirationTime?: number /** Configuration for 'i18next-fetch-backend' */ From 8fc79754c2e8e84c616b3cb701fde55325f9f470 Mon Sep 17 00:00:00 2001 From: Thomas Ghysels Date: Tue, 9 May 2023 00:56:58 +0200 Subject: [PATCH 2/3] chore: upgrade dependencies of @bothrs/translations --- packages/translations/package.json | 16 ++++---- yarn.lock | 63 ++++++++++++++++-------------- 2 files changed, 42 insertions(+), 37 deletions(-) diff --git a/packages/translations/package.json b/packages/translations/package.json index 60f4fc9..6d485ad 100644 --- a/packages/translations/package.json +++ b/packages/translations/package.json @@ -48,19 +48,19 @@ }, "dependencies": { "i18next-async-storage-backend2": "^2.0.0", - "i18next-chained-backend": "^3.0.0", - "i18next-fetch-backend": "^3.0.0", - "i18next-localstorage-backend": "^3.1.3", - "i18next-multiload-backend-adapter": "^1.0.0" + "i18next-chained-backend": "^4", + "i18next-fetch-backend": "^4", + "i18next-localstorage-backend": "^4", + "i18next-multiload-backend-adapter": "^2" }, "peerDependencies": { - "i18next": "^20.0.0", + "i18next": "^22", "react": ">=16.x" }, "devDependencies": { - "@types/node": "^16.11.6", - "@types/react": "~17.0.44", - "i18next": "^20.0.0", + "@types/node": "^20", + "@types/react": "^18", + "i18next": "^22", "parcel": "^2.8.3", "tslib": "^2.3.1", "typescript": "~4.6.4" diff --git a/yarn.lock b/yarn.lock index 7f0a6b8..02c9773 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1123,10 +1123,10 @@ resolved "https://registry.yarnpkg.com/@babel/regjsgen/-/regjsgen-0.8.0.tgz#f0ba69b075e1f05fb2825b7fad991e7adbb18310" integrity sha512-x/rqGMdzj+fWZvCOYForTghzbtqPDZ5gPwaoNGHdgDfF2QA/XZbCBp4Moo5scrkAMPhB7z26XM/AaHuIJdgauA== -"@babel/runtime@^7.12.0", "@babel/runtime@^7.14.6", "@babel/runtime@^7.15.4", "@babel/runtime@^7.16.0", "@babel/runtime@^7.18.9", "@babel/runtime@^7.20.7", "@babel/runtime@^7.8.4": - version "7.21.0" - resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.21.0.tgz#5b55c9d394e5fcf304909a8b00c07dc217b56673" - integrity sha512-xwII0//EObnq89Ji5AKYQaRYiW/nZ3llSv29d49IuxPhKbtJoLP+9QUUZ4nVragQVtaVGeZrpB+ZtG/Pdy/POw== +"@babel/runtime@^7.15.4", "@babel/runtime@^7.16.0", "@babel/runtime@^7.19.4", "@babel/runtime@^7.20.6", "@babel/runtime@^7.20.7", "@babel/runtime@^7.8.4": + version "7.21.5" + resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.21.5.tgz#8492dddda9644ae3bda3b45eabe87382caee7200" + integrity sha512-8jI69toZqqcsnqGGqwGS4Qb1VwLOEp4hz+CXPywcvjs60u3B4Pom/U/7rm4W8tMOYEB+E9wgD0mW1l3r8qlI9Q== dependencies: regenerator-runtime "^0.13.11" @@ -3493,11 +3493,16 @@ resolved "https://registry.yarnpkg.com/@types/node/-/node-18.15.12.tgz#833756634e78c829e1254db006468dadbb0c696b" integrity sha512-Wha1UwsB3CYdqUm2PPzh/1gujGCNtWVUYF0mB00fJFoR4gTyWTDPjSm+zBF787Ahw8vSGgBja90MkgFwvB86Dg== -"@types/node@^16.11.6", "@types/node@^16.9.0": +"@types/node@^16.9.0": version "16.18.24" resolved "https://registry.yarnpkg.com/@types/node/-/node-16.18.24.tgz#f21925dd56cd3467b4e1e0c5071d0f2af5e9a316" integrity sha512-zvSN2Esek1aeLdKDYuntKAYjti9Z2oT4I8bfkLLhIxHlv3dwZ5vvATxOc31820iYm4hQRCwjUgDpwSMFjfTUnw== +"@types/node@^20": + version "20.1.1" + resolved "https://registry.yarnpkg.com/@types/node/-/node-20.1.1.tgz#afc492e8dbe7f672dd3a13674823522b467a45ad" + integrity sha512-uKBEevTNb+l6/aCQaKVnUModfEMjAl98lw2Si9P5y4hLu9tm6AlX2ZIoXZX6Wh9lJueYPrGPKk5WMCNHg/u6/A== + "@types/node@~16.11.38": version "16.11.68" resolved "https://registry.yarnpkg.com/@types/node/-/node-16.11.68.tgz#30ee923f4d940793e0380f5ce61c0bd4b7196b6c" @@ -3530,7 +3535,7 @@ dependencies: "@types/react" "^17" -"@types/react@*", "@types/react@^17", "@types/react@~17.0.44": +"@types/react@*", "@types/react@^17", "@types/react@^18", "@types/react@~17.0.44": version "17.0.58" resolved "https://registry.yarnpkg.com/@types/react/-/react-17.0.58.tgz#c8bbc82114e5c29001548ebe8ed6c4ba4d3c9fb0" integrity sha512-c1GzVY97P0fGxwGxhYq989j4XwlcHQoto6wQISOC2v6wm3h0PORRWJFHlkRjfGsiG3y1609WdQ+J+tKxvrEd6A== @@ -7688,36 +7693,36 @@ i18next-async-storage-backend2@^2.0.0: resolved "https://registry.yarnpkg.com/i18next-async-storage-backend2/-/i18next-async-storage-backend2-2.0.1.tgz#cd1fed4cc426106ecf957c7a10f45509a6002bfa" integrity sha512-VIGKPr4bpe7K07GG+kS3mu8nNmttYbY2aty8xEMNtNIEoSVspo+HAOzPVbApV41zjnj/+driHD805wS9PyHDoA== -i18next-chained-backend@^3.0.0: - version "3.1.0" - resolved "https://registry.yarnpkg.com/i18next-chained-backend/-/i18next-chained-backend-3.1.0.tgz#b37d2980598deb8ec5656eba0df8fe15d3d62f1f" - integrity sha512-ltWy0fPMrtXjq0rSLo7s4ZF92LGvVsO47hhV3czaZXzMzhpFva6LVKMyDT7x82R8vAyB7VAeyGLBvMiW4W543A== +i18next-chained-backend@^4: + version "4.2.0" + resolved "https://registry.yarnpkg.com/i18next-chained-backend/-/i18next-chained-backend-4.2.0.tgz#8d2ccba6f2faca14d2bc5023ba61c759215b99a9" + integrity sha512-p3qhv6GMouhpcFPnOBqSDsQyVrwZu+PKr+kXaEAGE+w9O39t4pbpBsFYjxh4Nvhn8MUEXstaDx4ZuQvsNlI5fg== dependencies: - "@babel/runtime" "^7.18.9" + "@babel/runtime" "^7.19.4" -i18next-fetch-backend@^3.0.0: - version "3.0.0" - resolved "https://registry.yarnpkg.com/i18next-fetch-backend/-/i18next-fetch-backend-3.0.0.tgz#1f6a4c23c81141c8db52c751e989500db4c8d038" - integrity sha512-UiK9dI81qzEtnhgpKcRPAo67ChYaUaIi1kfRfN+vuFEoP3SPttuIY1RJAnVh7qaYzS573cZtG/7KE+qq5Dn1YQ== +i18next-fetch-backend@^4: + version "4.1.3" + resolved "https://registry.yarnpkg.com/i18next-fetch-backend/-/i18next-fetch-backend-4.1.3.tgz#738ab985f8c77aff85319d0bd57e0d40f0b28888" + integrity sha512-y2QBzUpMlF6oDUGfpUbaXp2OeTrKiPTk2OmTSN4ys/HpZ8NTPpXeBgLhrGAir9Jmq3UFabvsqn+BUWZ65jedbQ== -i18next-localstorage-backend@^3.1.3: - version "3.1.3" - resolved "https://registry.yarnpkg.com/i18next-localstorage-backend/-/i18next-localstorage-backend-3.1.3.tgz#5eaad25a515bdadebeb13e1486acfa6fa1686cbe" - integrity sha512-tx8dxQTEsTnRC654IrXPFr94c3NH7bIVHGKHnGvbgefpLz13/uFT5ITsmhqhg/gOza0TIj8e5jTsGnQytIhh+A== +i18next-localstorage-backend@^4: + version "4.1.1" + resolved "https://registry.yarnpkg.com/i18next-localstorage-backend/-/i18next-localstorage-backend-4.1.1.tgz#a27d38b3e933f911c03bfabc03ebd889b90eaf2b" + integrity sha512-gz3OP6m0jygF2SAzarDSFpaziYzF8mS8DP/pEUze+kDquzHTz2sy/eshcSkmFEYyGXMC037xUtlT97bNGlpZPA== dependencies: - "@babel/runtime" "^7.14.6" + "@babel/runtime" "^7.20.6" -i18next-multiload-backend-adapter@^1.0.0: - version "1.0.0" - resolved "https://registry.yarnpkg.com/i18next-multiload-backend-adapter/-/i18next-multiload-backend-adapter-1.0.0.tgz#3cc3ea102814273bb9059a317d04a3b6e4316121" - integrity sha512-rZd/Qmr7KkGktVgJa78GPLXEnd51OyB2I9qmbI/mXKPm3MWbXwplIApqmZgxkPC9ce+b8Jnk227qX62W9SaLPQ== +i18next-multiload-backend-adapter@^2: + version "2.2.0" + resolved "https://registry.yarnpkg.com/i18next-multiload-backend-adapter/-/i18next-multiload-backend-adapter-2.2.0.tgz#79fc716f3027aaec2fdacb5f566b1caf4d18ce46" + integrity sha512-V7or5zeiCMjWC45vFrlkNgef+y4PH9B2PGcoRZnypjnPIn9cFoEEaJoqHXIFnKsZeyjzFDrhAwE57GFyNVyW1g== -i18next@^20.0.0: - version "20.6.1" - resolved "https://registry.yarnpkg.com/i18next/-/i18next-20.6.1.tgz#535e5f6e5baeb685c7d25df70db63bf3cc0aa345" - integrity sha512-yCMYTMEJ9ihCwEQQ3phLo7I/Pwycf8uAx+sRHwwk5U9Aui/IZYgQRyMqXafQOw5QQ7DM1Z+WyEXWIqSuJHhG2A== +i18next@^22: + version "22.4.15" + resolved "https://registry.yarnpkg.com/i18next/-/i18next-22.4.15.tgz#951882b751872994f8502b5a6ef6f796e6a7d7f8" + integrity sha512-yYudtbFrrmWKLEhl6jvKUYyYunj4bTBCe2qIUYAxbXoPusY7YmdwPvOE6fx6UIfWvmlbCWDItr7wIs8KEBZ5Zg== dependencies: - "@babel/runtime" "^7.12.0" + "@babel/runtime" "^7.20.6" iconv-lite@0.4.24, iconv-lite@^0.4.24: version "0.4.24" From 842a1cfae9c89cb0b62bee31c02a53fab8ebf3bd Mon Sep 17 00:00:00 2001 From: Thomas Ghysels Date: Tue, 9 May 2023 01:26:29 +0200 Subject: [PATCH 3/3] feat: keep i18next in sync with language passed to useTranslations BREAKING: in specific cases, but should behave more within expectations than before. --- packages/translations/src/index.ts | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/packages/translations/src/index.ts b/packages/translations/src/index.ts index bf2b9a6..e13efe9 100644 --- a/packages/translations/src/index.ts +++ b/packages/translations/src/index.ts @@ -16,27 +16,28 @@ import type { TranslationRow, } from './types' -let initializing = false +let initializing: string | undefined /** * A hook which initializes i18next and loads the languages from an external data source * @param initParams takes fetch options cache expiration time and all i18next params - * @returns boolean which indicates if the languages are loaded + * @returns string or undefined which indicates the language that was loaded */ -export function useTranslations( - initParameters: TranslationInitParameters -): boolean { - const [initialized, setInitialized] = useState(i18next.isInitialized || false) +export function useTranslations(initParameters: TranslationInitParameters) { + const [initialized, setInitialized] = useState( + i18next.isInitialized ? initParameters.lng : undefined + ) + const lng = initParameters.lng useEffect(() => { if (!initializing && !i18next.isInitialized) { - initializing = true - initTranslations(initParameters).then(() => { - setInitialized(true) - initializing = false - }) + initializing = lng + initTranslations(initParameters).then(() => setInitialized(initializing)) + } else if (initializing !== lng) { + initializing = lng + i18next.changeLanguage(lng).then(() => setInitialized(initializing)) } - }, []) + }, [lng]) return initialized }