Skip to content

Commit

Permalink
refactor: isolate i18n configuration in api
Browse files Browse the repository at this point in the history
  • Loading branch information
bpetetot committed Nov 6, 2024
1 parent db7e130 commit f2ce8ed
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 45 deletions.
31 changes: 31 additions & 0 deletions api/src/shared/infrastructure/i18n/i18n.js
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;
}
19 changes: 3 additions & 16 deletions api/src/shared/infrastructure/plugins/i18n.js
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 };
37 changes: 37 additions & 0 deletions api/tests/shared/unit/infrastructure/i18n/i18n_test.js
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);
});
});
});
20 changes: 0 additions & 20 deletions api/tests/tooling/i18n/i18n.js

This file was deleted.

9 changes: 0 additions & 9 deletions api/tests/tooling/i18n/i18n_test.js

This file was deleted.

0 comments on commit f2ce8ed

Please sign in to comment.