fix translation init

This commit is contained in:
Paul Coral
2025-08-06 21:58:14 +02:00
parent 333fd824e3
commit fece4dcee1
4 changed files with 25 additions and 19 deletions

View File

@@ -3,6 +3,9 @@
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"deleteOutDir": true
"deleteOutDir": true,
"assets": [
{ "include": "assets/**/*", "watchAssets": true }
]
}
}

View File

@@ -6,24 +6,27 @@ import * as i18nextBackend from 'i18next-fs-backend';
export const LOCALIZATION_SERVICE = 'LOCALIZATION_SERVICE';
export const localizationServiceProvider: FactoryProvider = {
export const localizationServiceProvider: FactoryProvider<LocalizationService> =
{
provide: LOCALIZATION_SERVICE,
scope: Scope.REQUEST,
useFactory: async (req: Request) => {
const instance = i18next.createInstance().use(i18nextBackend as any);
const supportedLang =
req.acceptsLanguages().find((l) => isSupportedLang(l)) ?? 'fr';
const supportedLang = req
.acceptsLanguages()
.find((l) => isSupportedLang(l));
await instance.init<i18nextBackend.FsBackendOptions>({
lng: supportedLang,
debug: true,
fallbackLng: 'en',
debug: false,
backend: {
loadPath: `${__dirname}/src/assets/i18n/{{lng}}.json`,
loadPath: `assets/i18n/{{lng}}.json`,
},
});
new LocalizationService(instance);
return new LocalizationService(instance);
},
inject: ['REQUEST'],
};
};
export class LocalizationService {
constructor(private readonly i18n: i18next.i18n) {}