Translation docstrings with gettext

sapient aleksey.rezvov at gmail.com
Tue Apr 6 11:52:57 CEST 2010


Lie Ryan, thank you for your answer!
> Why would you want to translate docstring? Docstring is meant for
> developers not users.
I have mentioned that I want to provide API for existing image-
processing applicaion in Python.
In my case "developers" are "users".
Python provides great possibilities for documenting objects, I want to
use it for things such context help about API modules, classes,
methods, and want to do it in several languages.

> Maintaining a translated docstring is going to be
> a maintenance hell and will either hampers your application's agility or
> you will be left with obsolete docstrings in various languages you don't
> know.
You are right, there are problems here, but there are advantages too
(we are talking about API): developers can write documentation in
thier "cracked" English (like my), but professional translator can
correct it after with Poedit.

> Anyway, my job is to answer questions, not question the economic
> feasibility of your decision, so try this:
>
> #!python
> __doc__ = _("""testmodule docstring""")
>
> class TestClass:
>     __doc__ = _("""testmodule.TestClass docstring""")
>
>     def testClassMethod(self):
>         __doc__ = _("""testmodule.TestClass.testClassMethod docstring""")
>         print _("Call TestClass.testClassMethod()")
Yes, I tried this method, but it does not work with methods and
functions, this line
>         __doc__ = _("""testmodule.TestClass.testClassMethod docstring""")
does nothing (There are not output in help()). Is it any way to assign
docstring for function explicity?

> If you want to avoid having the explicit assignment to __doc__, you can
> also try using some metaclass or decorator magic to automatically wraps
> docstring in a _() call.

Yes, it will be better to avoid them, is it any existing tool/lib/
workaround that can do it?
I am follower of aproved, stable solutions, do not like to reinvent
the wheel.



More information about the Python-list mailing list