Closed Susccy closed 1 year ago
would you like to create a PR
noted, will do if I have time
@rubiin I just discovered the defaultValue
option for the translate method which solves this problem for me. I didn't know it existed previously because it isn't documented anywhere. So a possible solution for this issue could be to simply add documentation for that option.
Added a section on getting started with all the translate option. This should help anyone out with similar issue https://nestjs-i18n.com/quick-start#translate-options
Clear and concise description of the problem
If I read the source code correctly, the current implementation of the
translate
method will return the translation key if no translation could be found in any of the fallback languages: https://github.com/toonvanstrijp/nestjs-i18n/blob/main/src/services/i18n.service.ts#L125In my code however I sometimes want to provide inline fallback strings to display when a dynamic translation doesn't exist. Currently I need to do that like this:
Suggested solution
It would be way more convenient if the
translate
method returnednull
orundefined
instead of the key in this case so I could do it like this:Being able to achieve this in one line is a huge advantage over always having to add another conditional check.
Alternative
No response
Additional context
A module config option to enable nullish returns (e.g.
useNullReturn: boolean
) would allow opt-in for this functionality and not cause any breaking changes with existing code.Validations