toonvanstrijp / nestjs-i18n

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

Custom resolver, custom context, nested translations without argumets #470

Open armpogart opened 1 year ago

armpogart commented 1 year ago

I have project where I have 2 scenarios, which are both impossible as far as I see currently with this module:

Any way to implement custom context, and/or custom interceptor which will work for my cases?

P.S. The fallback solution for me now, is just use some language service, which will abstract away resolvers functionality and provide the lang directly from language service when using I18nService, but with that solution I can't get nested translations (without arguments) to work anyways. The nested keys are not getting translated at all.

armpogart commented 1 year ago

For translating nested translations without arguments, I'm not even sure how it's supposed to work. There is a part in the I18nService that always checks for args before trying to translate nested translations:

https://github.com/toonvanstrijp/nestjs-i18n/blob/9303683da7f6648986bc1fd949679dafdb3db4aa/src/services/i18n.service.ts#L217

Susccy commented 1 year ago

the nested translations without args supposedly got fixed with #278 but I still have this same issue in 10.2.6:

// translations.json
{
  "nestedValue": "world",
  "foo": "Hello $t(translations.nestedValue)"
}
// foo.service.ts
i18n.translate("translations.foo", { args: {} }) // result: "Hello world"
i18n.translate("translations.foo") // result: "Hello $t(translations.nestedValue)"

🤔

Susccy commented 1 year ago

Also just discovered this unit test which explicitly states that nested without args should return the template, not the actual translation: https://github.com/toonvanstrijp/nestjs-i18n/blob/main/tests/i18n-express.e2e.spec.ts#L565-L569

rubiin commented 1 year ago

I think if there is a test for it that specifies that the nested translations without the args should return the template itself, then I suppose this is the desired implementation. To overcome this, you can always pass empty args like


i18n.translate("translations.foo", { args: {} }) // result: "Hello world"
Susccy commented 11 months ago

@rubiin that would be a very odd expected behavior and is also nowhere documented. Also I am still wondering why #278 says it was fixed then, because it clearly wasn't (or at least not anymore).

rubiin commented 11 months ago

@Susccy I think I will add this to documentation for now