Should we move from using underscore as builtin for translations? #5954
wenzeslaus
started this conversation in
Ideas
Replies: 1 comment 1 reply
-
I think that going more explicit would be better. It helps with the principle of less surprise. Also, I would inspire myself of the practices developed for Django. https://docs.djangoproject.com/en/5.2/topics/i18n/translation/ |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Background
In Python, we use the builtin package to make the translate function generally available global, i.e., a builtin (
python/grass/__init__.py
):The issue is that underscore is used for other things in Python, namely the interactive shell uses that to store the last value. This makes the translation function calls fail when used in an interactive console. This also makes the tests fail, although I made an effort to mitigate that with do_doctest_gettext_workaround which resolves at least some of the cases. Another usage of underscore is for an unused variable, so when someone uses that they overwrite the translation function locally and the next translation attempt fails creating confusion when writing new Python tools.
Here is failing code I spotted in the CI. What happens is that latest result is assigned to underscore and so calling
_("...")
fails with TypeError, here'bool' object is not callable
:If I recall my testing way back when correctly, importing the translation function fixes the problems with conflicts even when keeping the name as underscore.
Question
Should we keep using the translation function as a builtin and fix the individual issues in tests or should we review the whole code base and put there explicit imports of the translation function?
History
The history in reverse chronological order of this is that most recently (2021), I added the lazy loading so that the package can be imported before gettext is set up in f4313c4 (#1838). Before that (2019), bb779c2 (Trac Ticket 3790) made it a builtin as a preparation for never-finished Trac Ticket 3772: Make the grass library importable outside of a GRASS session. This replaced my earlier solution for wxGUI code which went into the opposite direction and made the translate function something to be imported, replacing the original solution using a builtin (which bb779c2 reverted to using a builtin again). The commit for the removal of builtin use in wxGUI is ee95f55 with additional fixes in 1423e79. The Trac Ticket 1739: Language switch on wxGUI doesn't affect all strings (fixed)) has an overview where things were in 2014:
I'm suggesting to go back to the 2014 solution of not using a builtin and instead, have an explicit import anywhere where translation function is needed.
Beta Was this translation helpful? Give feedback.
All reactions