toonvanstrijp / nestjs-i18n

The i18n module for nestjs.
https://nestjs-i18n.com
Other
627 stars 101 forks source link

I18nContext.current() returns undefined with disableMiddleware: true #528

Open Susccy opened 11 months ago

Susccy commented 11 months ago

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

System:
    OS: Windows 10 10.0.19045
    CPU: (8) x64 11th Gen Intel(R) Core(TM) i7-1185G7 @ 3.00GHz
    Memory: 4.88 GB / 31.71 GB
  Binaries:
    Node: 18.12.1 - ~\AppData\Local\pnpm\node.EXE
    npm: 8.19.2 - ~\AppData\Local\pnpm\npm.CMD
  Browsers:
    Edge: Spartan (44.19041.1266.0), Chromium (115.0.1901.188)

Used Package Manager

npm

Validations

ChoSeoHwan commented 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.

gijsmin commented 11 months ago

Also experiencing this issue when using I18nContext.current() in a middleware

rubiin commented 11 months ago

@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

Susccy commented 11 months ago

@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.

rubiin commented 11 months ago

Sure thing @Susccy I also needed your input on another issue .