-
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.
[TECH] Isoler la configuration du module i18n de l'api
- Loading branch information
Showing
57 changed files
with
178 additions
and
101 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
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,54 @@ | ||
import path from 'node:path'; | ||
|
||
import { I18n } from 'i18n'; | ||
|
||
import { SUPPORTED_LOCALES } from '../../domain/constants.js'; | ||
import { logger } from '../utils/logger.js'; | ||
|
||
const __dirname = import.meta.dirname; | ||
const translationsFolder = path.resolve(path.join(__dirname, '../../../../translations')); | ||
|
||
const supportedLocales = SUPPORTED_LOCALES.map((supportedLocale) => supportedLocale.toLowerCase()); | ||
|
||
const DEFAULT_LOCALE = 'fr'; | ||
|
||
export const options = { | ||
locales: ['en', 'fr', 'es', 'nl'], | ||
fallbacks: { 'en-*': 'en', 'fr-*': 'fr', 'es-*': 'es', 'nl-*': 'nl' }, | ||
defaultLocale: DEFAULT_LOCALE, | ||
directory: translationsFolder, | ||
queryParameter: 'lang', | ||
languageHeaderField: 'Accept-Language', | ||
objectNotation: true, | ||
updateFiles: false, | ||
mustacheConfig: { | ||
tags: ['{', '}'], | ||
disable: false, | ||
}, | ||
}; | ||
|
||
// This is an optimization to avoid settings a new instance each time | ||
// we need to use i18n. | ||
const i18nInstances = {}; | ||
|
||
/** | ||
* @param {string} locale a locale (language or BCP 47 format) | ||
* @returns i18n instance correctly setup with the language | ||
*/ | ||
export function getI18n(locale) { | ||
if (!locale || !supportedLocales.includes(locale?.toLowerCase())) { | ||
return getI18n(DEFAULT_LOCALE); | ||
} | ||
|
||
if (!i18nInstances[locale]) { | ||
const i18n = new I18n(options); | ||
i18n.setLocale(locale); | ||
// we freeze the setLocale to avoid changing i18n locale for an instance | ||
i18n.setLocale = () => { | ||
logger.warn('Cannot change i18n locale instance, use getI18n(locale) instead.'); | ||
}; | ||
i18nInstances[locale] = i18n; | ||
} | ||
|
||
return i18nInstances[locale]; | ||
} |
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
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
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
2 changes: 1 addition & 1 deletion
2
api/tests/certification/enrolment/unit/application/attendance-sheet-controller_test.js
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
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
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
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
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
3 changes: 2 additions & 1 deletion
3
...rastructure/utils/csv/certification-results/get-session-certification-results-csv_test.js
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
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
2 changes: 1 addition & 1 deletion
2
...tests/certification/results/unit/application/certification-attestation-controller_test.js
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
2 changes: 1 addition & 1 deletion
2
api/tests/certification/results/unit/application/certification-controller_test.js
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
2 changes: 1 addition & 1 deletion
2
api/tests/certification/results/unit/application/certification-results-controller_test.js
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
2 changes: 1 addition & 1 deletion
2
api/tests/certification/results/unit/application/organization-controller_test.js
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
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
2 changes: 1 addition & 1 deletion
2
...unit/infrastructure/utils/csv/certification-results/CertificationResultsCsvValues_test.js
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
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
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
2 changes: 1 addition & 1 deletion
2
...s/certification/session-management/unit/application/jury-certification-controller_test.js
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
2 changes: 1 addition & 1 deletion
2
.../session-management/unit/infrastructure/serializers/jury-certification-serializer_test.js
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
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
2 changes: 1 addition & 1 deletion
2
api/tests/identity-access-management/integration/domain/usecases/create-user.usecase.test.js
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
2 changes: 1 addition & 1 deletion
2
api/tests/identity-access-management/unit/application/user/user.controller.test.js
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
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
Oops, something went wrong.