From bbda6eb896957166f2c846008b1559a613fd219b Mon Sep 17 00:00:00 2001 From: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> Date: Thu, 11 Jan 2024 21:39:44 +0530 Subject: [PATCH] =?UTF-8?q?fix(i18n):=20=F0=9F=A9=B9=20Not=20loading=20loc?= =?UTF-8?q?alStorage=20backend=20if=20it=20doesn't=20exist.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> --- src/i18n.js | 43 +++++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/src/i18n.js b/src/i18n.js index e405600ec..f13f89898 100644 --- a/src/i18n.js +++ b/src/i18n.js @@ -12,6 +12,29 @@ import getValidLocaleCode from './lib/i18n-localeParser.js' const { version } = pkgJson export const localesList = Object.values(locales) +let i18nBackend = [HttpBackend] +let i18nBackendOptions = [ + { // HttpBackend + loadPath: (lngs, namespaces) => { + const locale = getValidLocaleCode({ i18n, localeCode: lngs[0], languages: locales }) + // ensure a relative path is used to look up the locales, so it works when loaded from /ipfs/ + return `locales/${locale}/${namespaces}.json` + } + } +] + +if (typeof window !== 'undefined' && 'localStorage' in window) { + i18nBackend = [LocalStorageBackend, ...i18nBackend] + i18nBackendOptions = [ + { // LocalStorageBackend + defaultVersion: version, + expirationTime: (!process.env.NODE_ENV || process.env.NODE_ENV === 'development') ? 1 : 7 * 24 * 60 * 60 * 1000, + store: typeof window !== 'undefined' && 'localStorage' in window ? window.localStorage : null + }, + ...i18nBackendOptions + ] +} + i18n .use(ICU) .use(Backend) @@ -19,24 +42,8 @@ i18n .init({ load: 'currentOnly', // see https://github.com/i18next/i18next-http-backend/issues/61 backend: { - backends: [ - LocalStorageBackend, - HttpBackend - ], - backendOptions: [ - { // LocalStorageBackend - defaultVersion: version, - expirationTime: (!process.env.NODE_ENV || process.env.NODE_ENV === 'development') ? 1 : 7 * 24 * 60 * 60 * 1000 - }, - { // HttpBackend - loadPath: (lngs, namespaces) => { - const locale = getValidLocaleCode({ i18n, localeCode: lngs[0], languages: locales }) - // ensure a relative path is used to look up the locales, so it works when loaded from /ipfs/ - return `locales/${locale}/${namespaces}.json` - } - } - ] - }, + backends: i18nBackend, + backendOptions: i18nBackendOptions, ns: ['app', 'welcome', 'status', 'files', 'explore', 'peers', 'settings', 'notify'], defaultNS: 'app', fallbackNS: 'app',