Open Brakebein opened 1 year ago
@Brakebein so it only gives this error while running tests and not in normal conditions?
Yes, exactly.
As a workaround, I managed to mock the creation of the I18nContext. As long as I don't test that the actual translations are correct according to the language key, this works so far and the test doesn't fail.
const i18nService = app.get(I18nService);
jest.spyOn(I18nContext, 'current').mockImplementation(() => new I18nContext('de', i18nService));
I am wrapping the i18n module in my own module that has it's own t
function that is already typed (so I don't need to import types in multiple places.
I am running into the same issue: when I use my module in a controller it translates nicely, but if I create a testing module from my AppModule and try to use the app .get
to inject my service, the context's current
function returns undefined
.
@Brakebein @matzeso apologies if I've missed something in the documentation, but, can you post an example (top-to-bottom) of using the i18nService
in a context other than http request or test? For example, say I have a @Cron
task that simply outputs notifications on a regular schedule in x-language?
How does one successfully instantiate the i18n
service for common use in basically any "service.ts" context? I imagine something like: I18nContext.current().i18n.t...
but that somehow also seems wrong. 🤔
Sorry, I don't have any use case for @Cron
together with i18n. But shouldn't it be possible to just instantiate the I18nContext (as it works in my mocked implementation above)?
@Injectable()
export class TasksService {
private readonly logger = new Logger(TasksService.name);
constructor(@Inject(I18nService) private readonly i18nService: I18nService) {}
@Cron('45 * * * * *')
async handleCron() {
const i18n = new I18nContext('en', this.i18nService);
this.logger.debug(await i18n.t('key'));
}
}
@Brakebein if you are manually specifying language, you dont need the context. You can do
return this.i18n.t('test.HELLO',{ lang: 'en' });
I'm getting once again the error that the I18n context couldn't be found (#435).
The microservice works nicely. But I now want to implement some e2e tests, and when executing the test, the error is thrown and the test fails.
The app successfully initializes, but as soon as I call
client.send()
, the error is thrown.I'm using version
nestjs-i18n@10.2.6
.