diff --git a/src/i18n.js b/src/i18n.js index e405600ec..b54af2f89 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,23 +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',