Marking translatable strings

Bernhard Herzog herzog at online.de
Sun Sep 19 08:16:19 EDT 1999


"Barry A. Warsaw" <bwarsaw at cnri.reston.va.us> writes:

> I've posted about my pygettext.py module in response to other messages
> on this thread.  It occurs to me that doc strings would have to be
> handled differently, because if you were to stick a _() around a
> docstring, it ceases to /be/ a docstring!  I think it's important that 
> docstrings be translatable though so you'd have to have some
> post-processing step, either explicitly, or through an import hook of
> some sort (and that might be expensive to do if you wanted to pick up
> func docstrings in addition to module docstrings).

As I see it, there are two separate issues, marking the docstrings for
translation so that e.g. pygettext can extract them and actually
translating them.

The first part is easy, at least conceptually. Docstrings are meant for
documentation and are therefore meant to be translated in a localized
program almost by definition. pygettext would just have to be able to
recognize docstrings.

The second part can be tricky in some situations. To begin with, I'd say
that docstrings should not be translated automagically when a module is
imported for performance reasons, and perhaps more important, if
translation work similar to gettext, the program has to set the domain
before any strings can be translated, but by the time that is done, its
likely that some modules are already imported without translated
docstrings. It would also make switching languages at runtime
impossible.

Therefore, docstrings should be translated when they're accessed, and
I'd argue that they should be translated by whatever tool you use to
access and process them. The only situation I can think off where
explicit translation of docstrings is at least awkward is in an
interactive python session when you do things like 'print sys.__doc__'
to read a modules documentation.

Implementing automatic translation of docstrings on access seems to be
quite simple at first glance, you'd `just' have to change the getattr
methods of modules, classes and functions, but I fear there could be
some very tricky issues. E.g. if I'm not mistaken, a module's __doc__ is
just a normal global variable and functions reference the module's
__dict__ directly to look up globals. That means that the evaluation of
the global variable __doc__ in a function won't go through the module,
so you'd have to treat dict-entries for the __doc__ key specially or
generate special bytecode. Both is not desirable IMO.


-- 
Bernhard Herzog	  | Sketch, a drawing program for Unix
herzog at online.de  | http://www.online.de/home/sketch/




More information about the Python-list mailing list