Skip to content

Commit 4bebefa

Browse files
committed
fix(i18n): 🩹 Not loading localStorage backend if it doesn't exist.
Signed-off-by: Nishant Arora <[email protected]>
1 parent b8a71d9 commit 4bebefa

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

‎src/i18n.js

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,38 @@ import getValidLocaleCode from './lib/i18n-localeParser.js'
1212
const { version } = pkgJson
1313
export const localesList = Object.values(locales)
1414

15+
let i18nBackend = [HttpBackend]
16+
let i18nBackendOptions = [
17+
{ // HttpBackend
18+
loadPath: (lngs, namespaces) => {
19+
const locale = getValidLocaleCode({ i18n, localeCode: lngs[0], languages: locales })
20+
// ensure a relative path is used to look up the locales, so it works when loaded from /ipfs/<cid>
21+
return `locales/${locale}/${namespaces}.json`
22+
}
23+
}
24+
]
25+
26+
if (typeof window !== 'undefined' && 'localStorage' in window) {
27+
i18nBackend = [LocalStorageBackend, ...i18nBackend]
28+
i18nBackendOptions = [
29+
{ // LocalStorageBackend
30+
defaultVersion: version,
31+
expirationTime: (!process.env.NODE_ENV || process.env.NODE_ENV === 'development') ? 1 : 7 * 24 * 60 * 60 * 1000,
32+
store: typeof window !== 'undefined' && 'localStorage' in window ? window.localStorage : null
33+
},
34+
...i18nBackendOptions
35+
]
36+
}
37+
1538
i18n
1639
.use(ICU)
1740
.use(Backend)
1841
.use(LanguageDetector)
1942
.init({
2043
load: 'currentOnly', // see https://github.com/i18next/i18next-http-backend/issues/61
2144
backend: {
22-
backends: [
23-
LocalStorageBackend,
24-
HttpBackend
25-
],
26-
backendOptions: [
27-
{ // LocalStorageBackend
28-
defaultVersion: version,
29-
expirationTime: (!process.env.NODE_ENV || process.env.NODE_ENV === 'development') ? 1 : 7 * 24 * 60 * 60 * 1000
30-
},
31-
{ // HttpBackend
32-
loadPath: (lngs, namespaces) => {
33-
const locale = getValidLocaleCode({ i18n, localeCode: lngs[0], languages: locales })
34-
// ensure a relative path is used to look up the locales, so it works when loaded from /ipfs/<cid>
35-
return `locales/${locale}/${namespaces}.json`
36-
}
37-
}
38-
]
45+
backends: i18nBackend,
46+
backendOptions: i18nBackendOptions
3947
},
4048
ns: ['app', 'welcome', 'status', 'files', 'explore', 'peers', 'settings', 'notify'],
4149
defaultNS: 'app',

0 commit comments

Comments
 (0)