From 315531ad53d0625e9c8230a1b27c8390e754edfb Mon Sep 17 00:00:00 2001 From: Alex McHardy Date: Tue, 13 Jul 2021 02:55:34 -0600 Subject: [PATCH] fix: Respect user-defined prefix/suffix when looking for default NS (#1248) * fix: Respect user-defined prefix/suffix when looking for default NS * Add test Co-authored-by: Isaac Hinman --- src/config/createConfig.test.ts | 22 +++++++++++++++++++--- src/config/createConfig.ts | 4 +++- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/config/createConfig.test.ts b/src/config/createConfig.test.ts index 966a3147..bb6c9f04 100644 --- a/src/config/createConfig.test.ts +++ b/src/config/createConfig.test.ts @@ -116,15 +116,31 @@ describe('createConfig', () => { }) }) - describe('when filesystem is missing defaultNS', () => { - it('throws an error', () => { + describe('defaultNS validation', () => { + it('when filesystem is missing defaultNS throws an error', () => { (fs.existsSync as jest.Mock).mockReturnValueOnce(false) const config = createConfig.bind(null, { lng: 'en', - } as any) + } as UserConfig) + + expect(config).toThrow('Default namespace not found at public/locales/en/common.json') + }) + + it('uses user provided prefix/suffix with localeStructure', () => { + (fs.existsSync as jest.Mock).mockReturnValueOnce(false) + + const config = createConfig.bind(null, { + interpolation: { + prefix: '^^', + suffix: '$$', + }, + lng: 'en', + localeStructure: '^^lng$$/^^ns$$', + } as UserConfig) expect(config).toThrow('Default namespace not found at public/locales/en/common.json') + expect(fs.existsSync).toHaveBeenCalledWith('public/locales/en/common.json') }) }) diff --git a/src/config/createConfig.ts b/src/config/createConfig.ts index 46fc0411..5c72a312 100644 --- a/src/config/createConfig.ts +++ b/src/config/createConfig.ts @@ -55,7 +55,9 @@ export const createConfig = (userConfig: UserConfig): InternalConfig => { // https://github.com/isaachinman/next-i18next/issues/358 // if (typeof defaultNS === 'string' && typeof lng !== 'undefined') { - const defaultLocaleStructure = localeStructure.replace('{{lng}}', lng).replace('{{ns}}', defaultNS) + const prefix = userConfig?.interpolation?.prefix ?? '{{' + const suffix = userConfig?.interpolation?.suffix ?? '}}' + const defaultLocaleStructure = localeStructure.replace(`${prefix}lng${suffix}`, lng).replace(`${prefix}ns${suffix}`, defaultNS) const defaultFile = `/${defaultLocaleStructure}.${localeExtension}` const defaultNSPath = path.join(localePath, defaultFile) const defaultNSExists = fs.existsSync(defaultNSPath)