I often run into the mistake of using gettext
in a context where the actual language is not set yet - instead of using the appropriate gettext_lazy
.
Those errors can be difficult to catch - I would like to make them more visible appropriate logging, or possibly even throwing exceptions.
- Would it be correct, to write a wrapper function that logs an error in the case of
get_language() is None
? - Then of course, one would need to enforce that this wrapper is always used - introducing another subtle easy to make mistake. In general, I suppose monkey-patching could be used, but I'd like to avoid that if possible. The whole dispatch behind
django.util.translation
seems already relatively complex. Is there a clean way too hook this functionality in there?
gettext
why not script a refactoring ofgettext
togettext_lazy
? – Functionalismdjango.utils.translation.gettext = django.utils.translation.gettext_lazy
– Prosegettext_lazy
overgettext
would be interesting, but doesn't fit in a comment. And it's not exactly the point of this question - even in that case, I am still reluctant to blindly use monkey-patching. And I even with a refactoring. I still have no means to enforce not using a standard Django function. – Gravelly