Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Respect user-defined prefix/suffix when looking for default NS #1248

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions src/config/createConfig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
})

Expand Down
4 changes: 3 additions & 1 deletion src/config/createConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down