Skip to content

Commit

Permalink
fix: Add server side loading of fallbackLng (#970)
Browse files Browse the repository at this point in the history
  • Loading branch information
isaachinman authored Feb 24, 2021
1 parent 84b9aba commit 409ce46
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
28 changes: 28 additions & 0 deletions src/config/createConfig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,34 @@ describe('createConfig', () => {
} as UserConfig)
expect((config.detection as any).hello).toEqual('world')
})

describe('fallbackLng', () => {
it('automatically sets if it user does not provide', () => {
const config = createConfig({
lng: 'en',
} as UserConfig)

expect(config.fallbackLng).toBe('en')
})

it('does not overwrite user provided value', () => {
const config = createConfig({
fallbackLng: 'hello-world',
lng: 'en',
} as UserConfig)

expect(config.fallbackLng).toBe('hello-world')
})

it('does not overwrite user provided boolean', () => {
const config = createConfig({
fallbackLng: false,
lng: 'en',
} as UserConfig)

expect(config.fallbackLng).toBe(false)
})
})
})

describe('when filesystem is missing defaultNS', () => {
Expand Down
4 changes: 4 additions & 0 deletions src/config/createConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ export const createConfig = (userConfig: UserConfig): InternalConfig => {
return combinedConfig as InternalConfig
}

if (typeof combinedConfig.fallbackLng === 'undefined') {
combinedConfig.fallbackLng = combinedConfig.defaultLocale
}

if (!process.browser) {
combinedConfig.preload = locales

Expand Down
12 changes: 9 additions & 3 deletions src/serverSideTranslations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,16 @@ export const serverSideTranslations = async (
[initialLocale]: {},
}

if (typeof config.fallbackLng === 'string') {
initialI18nStore[config.fallbackLng] = {}
}

namespacesRequired.forEach((ns) => {
initialI18nStore[initialLocale][ns] = (
(i18n.services.resourceStore.data[initialLocale] || {})[ns] || {}
)
for (const locale in initialI18nStore) {
initialI18nStore[locale][ns] = (
(i18n.services.resourceStore.data[locale] || {})[ns] || {}
)
}
})

return {
Expand Down

0 comments on commit 409ce46

Please sign in to comment.