-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: isolate i18n configuration in api
- Loading branch information
Showing
5 changed files
with
71 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import path from 'node:path'; | ||
import * as url from 'node:url'; | ||
|
||
import { I18n } from 'i18n'; | ||
|
||
const __dirname = url.fileURLToPath(new URL('.', import.meta.url)); | ||
const translationsFolder = path.resolve(path.join(__dirname, '../../../../translations')); | ||
|
||
export const options = { | ||
locales: ['en', 'fr', 'es', 'nl'], | ||
directory: translationsFolder, | ||
defaultLocale: 'fr', | ||
queryParameter: 'lang', | ||
languageHeaderField: 'Accept-Language', | ||
objectNotation: true, | ||
updateFiles: false, | ||
mustacheConfig: { | ||
tags: ['{', '}'], | ||
disable: false, | ||
}, | ||
}; | ||
|
||
export function getI18n() { | ||
return new I18n(options); | ||
} | ||
|
||
export function getI18nForLocale(locale) { | ||
const i18n = getI18n(); | ||
i18n.setLocale(locale); | ||
return i18n; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,7 @@ | ||
import * as url from 'node:url'; | ||
|
||
import hapiI18n from 'hapi-i18n'; | ||
const __dirname = url.fileURLToPath(new URL('.', import.meta.url)); | ||
|
||
import { options } from '../i18n/i18n.js'; | ||
|
||
const plugin = hapiI18n; | ||
const options = { | ||
locales: ['en', 'fr', 'es', 'nl'], | ||
directory: __dirname + '../../../../translations', | ||
defaultLocale: 'fr', | ||
queryParameter: 'lang', | ||
languageHeaderField: 'Accept-Language', | ||
objectNotation: true, | ||
updateFiles: false, | ||
mustacheConfig: { | ||
tags: ['{', '}'], | ||
disable: false, | ||
}, | ||
}; | ||
|
||
export { options, plugin }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { getI18n, getI18nForLocale, options } from '../../../../../src/shared/infrastructure/i18n/i18n.js'; | ||
import { expect } from '../../../../test-helper.js'; | ||
|
||
describe('Unit | Shared | Infrastucture | i18n', function () { | ||
describe('default i18n options', function () { | ||
it('returns i18n options', function () { | ||
expect(options).to.have.property('locales').that.includes('en', 'fr', 'es', 'nl'); | ||
expect(options).to.have.property('directory').that.is.a('string'); | ||
expect(options).to.have.property('defaultLocale', 'fr'); | ||
expect(options).to.have.property('queryParameter', 'lang'); | ||
expect(options).to.have.property('languageHeaderField', 'Accept-Language'); | ||
expect(options).to.have.property('objectNotation', true); | ||
expect(options).to.have.property('updateFiles', false); | ||
expect(options) | ||
.to.have.property('mustacheConfig') | ||
.to.deep.equal({ | ||
tags: ['{', '}'], | ||
disable: false, | ||
}); | ||
}); | ||
}); | ||
|
||
describe('getI18n', function () { | ||
it('returns an instance of I18n with default options', function () { | ||
const i18n = getI18n(); | ||
expect(i18n.getLocale()).to.equal('fr'); | ||
}); | ||
}); | ||
|
||
describe('getI18nForLocale', function () { | ||
it('returns an instance of I18n with the specified locale', function () { | ||
const locale = 'es'; | ||
const i18n = getI18nForLocale(locale); | ||
expect(i18n.getLocale()).to.equal(locale); | ||
}); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.