[GRASS-dev] [GRASS GIS] #3790: Cleanup gettext usage for python code
GRASS GIS
trac at osgeo.org
Tue Mar 26 20:28:07 PDT 2019
#3790: Cleanup gettext usage for python code
--------------------------+-------------------------
Reporter: pmav99 | Owner: grass-dev@…
Type: enhancement | Status: closed
Priority: normal | Milestone: 7.8.0
Component: Python | Version: svn-trunk
Resolution: fixed | Keywords:
CPU: Unspecified | Platform: Unspecified
--------------------------+-------------------------
Comment (by wenzeslaus):
The things in lib/python are potentially (and actually) used as Python
modules by other applications or scripts. Thus from
https://pymotw.com/2/gettext/index.html#module-localization, it is
actually the second option which applies:
> //For a library, or individual module, modifying `__builtins__` is not a
good idea because you don’t know what conflicts you might introduce with
an application global value. You can import or re-bind the names of
translation functions by hand at the top of your module.//
The doctests and interative command line are probably where the issues
will happen first. See Django practices from
https://docs.djangoproject.com/en/dev/topics/i18n/translation/ as linked
in #1739:
> //Python’s standard library gettext module installs _() into the global
namespace, as an alias for gettext(). In Django, we have chosen not to
follow this practice, for a couple of reasons: //
> //Sometimes, you should use gettext_lazy() as the default translation
method for a particular file. Without _() in the global namespace, the
developer has to think about which is the most appropriate translation
function.//
> //The underscore character (_) is used to represent “the previous
result” in Python’s interactive shell and doctest tests. Installing a
global _() function causes interference. Explicitly importing gettext() as
_() avoids this problem.//
To run doctests, use
{{{
python -m doctest -v example.py
}}}
or (often already included in code):
{{{
if __name__ == "__main__":
import doctest
doctest.testmod()
}}}
both taken from https://docs.python.org/2/library/doctest.html, so the
question is if you have an particular issue. Another things to test are
Python in command line, Python console in GUI, and Jupyter Notebook. (But
the doctest are where the issues definitively emerged in the past.)
Please note that I never understood all the details of #1739 as noted
there, so maybe we have things to investigate more. Also, try to examine
`do_doctest_gettext_workaround()` which doctests need when the builin is
modified or perhaps even in other cases.
Finally, is there any overhead in centralizing the tr string installation?
So far we were relatively successful in keeping the GUI separate (whether
for performance or code organization reasons).
--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3790#comment:4>
GRASS GIS <https://grass.osgeo.org>
More information about the grass-dev
mailing list