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 <[email protected]>
  • Loading branch information
whizzzkid committed Jan 11, 2024
1 parent b8a71d9 commit 4bebefa
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions src/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +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',
Expand Down

0 comments on commit 4bebefa

Please sign in to comment.