Open Susccy opened 11 months ago
I have same issue.
First of all, for service, after extracting request
instance from ExcutionContext
as below, access request.i18nService
and use it.
const request = getRequest<Request & { i18nService: I18nService }>(context);
const i18nService = request.i18nService;
i18nService.t('Error.MESSAGE');
I think it's a hacky way, and it basically needs to be modified in the library, but it works for now.
Also experiencing this issue when using I18nContext.current()
in a middleware
@Susccy If there was a way you could isolate the part of the code that causes the issue. Doesnt have to be a big thing just a small repo that addresses the issue
@rubiin for the time being I'm working around this problem by not using the I18nContext at all (no resolvers as well) and instead only using the I18nService with the lang
option set manually in every translate method call (with the locale passed down as a parameter from the controllers to the services).
If I'm ever able to isolate this problem (or someone here more experienced than me) I'll share my findings here.
Sure thing @Susccy I also needed your input on another issue .
Describe the bug
I'm trying to translate stuff in microservice providers but since the injectable I18nService always uses the fallback language (...what's even the purpose of the service if it's not using the resolved language?) I can't use that and instead do
I18nContext.current()
everywhere (good practice? idk).This was working fine until I needed to add a custom resolver that gets the user locale from the logged in session, which is only available on the ExecutionContext if I set
disableMiddleware: true
on the i18n module. However since I added that option,I18nContext.current()
now always returns undefined in any service and controller. Injecting the I18nContext in the controllers still works though (both@I18n
and@I18nLang
decorators work fine).So now I don't know how to translate in the microservices anymore because I can't get the resolved language from anywhere except in the controllers. Does that mean my only current option is to pass down the locale as a parameter to all service methods?
Reproduction
Haven't been able to create a minimal reproduction yet unfortunately. The error occurs in a large enterprise codebase and I'm still trying to trace it back to its origin at this time. Perhaps a simple microservice setup would suffice but I'm still new to NestJS and need to study this stuff first before I can build a working example myself. So if anyone experiences the same issue I'd appreciate your help on this.
System Info
Used Package Manager
npm
Validations