fix translation init
This commit is contained in:
@@ -3,6 +3,9 @@
|
||||
"collection": "@nestjs/schematics",
|
||||
"sourceRoot": "src",
|
||||
"compilerOptions": {
|
||||
"deleteOutDir": true
|
||||
"deleteOutDir": true,
|
||||
"assets": [
|
||||
{ "include": "assets/**/*", "watchAssets": true }
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,24 +6,27 @@ import * as i18nextBackend from 'i18next-fs-backend';
|
||||
|
||||
export const LOCALIZATION_SERVICE = 'LOCALIZATION_SERVICE';
|
||||
|
||||
export const localizationServiceProvider: FactoryProvider = {
|
||||
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';
|
||||
await instance.init<i18nextBackend.FsBackendOptions>({
|
||||
lng: supportedLang,
|
||||
debug: true,
|
||||
backend: {
|
||||
loadPath: `${__dirname}/src/assets/i18n/{{lng}}.json`,
|
||||
},
|
||||
});
|
||||
new LocalizationService(instance);
|
||||
},
|
||||
inject: ['REQUEST'],
|
||||
};
|
||||
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));
|
||||
await instance.init<i18nextBackend.FsBackendOptions>({
|
||||
lng: supportedLang,
|
||||
fallbackLng: 'en',
|
||||
debug: false,
|
||||
backend: {
|
||||
loadPath: `assets/i18n/{{lng}}.json`,
|
||||
},
|
||||
});
|
||||
return new LocalizationService(instance);
|
||||
},
|
||||
inject: ['REQUEST'],
|
||||
};
|
||||
|
||||
export class LocalizationService {
|
||||
constructor(private readonly i18n: i18next.i18n) {}
|
||||
|
||||
Reference in New Issue
Block a user