Skip to content

Commit

Permalink
fix(i18n): 🩹 Not loading localStorage backend if it doesn't exist.
Browse files Browse the repository at this point in the history
Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com>
  • Loading branch information
whizzzkid committed Jan 11, 2024
1 parent b8a71d9 commit bbda6eb
Showing 1 changed file with 25 additions and 18 deletions.
43 changes: 25 additions & 18 deletions src/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,38 @@ 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/<cid>
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)
.use(LanguageDetector)
.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/<cid>
return `locales/${locale}/${namespaces}.json`
}
}
]
},
backends: i18nBackend,
backendOptions: i18nBackendOptions,
ns: ['app', 'welcome', 'status', 'files', 'explore', 'peers', 'settings', 'notify'],
defaultNS: 'app',
fallbackNS: 'app',
Expand Down

0 comments on commit bbda6eb

Please sign in to comment.