From 5d593ed399b89c3026ee20d565aac0e1f5808216 Mon Sep 17 00:00:00 2001 From: Alex McHardy Date: Tue, 8 Jun 2021 09:59:32 -0500 Subject: [PATCH 1/2] fix: Respect user-defined prefix/suffix when looking for default NS --- src/config/createConfig.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/config/createConfig.ts b/src/config/createConfig.ts index dc753392..7a26c946 100644 --- a/src/config/createConfig.ts +++ b/src/config/createConfig.ts @@ -56,7 +56,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) From 0b3636cd21416716b654598d6184476305b716be Mon Sep 17 00:00:00 2001 From: Alex McHardy Date: Mon, 12 Jul 2021 13:08:57 -0500 Subject: [PATCH 2/2] Add test --- src/config/createConfig.test.ts | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/config/createConfig.test.ts b/src/config/createConfig.test.ts index dbac3eee..4054c108 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') }) })