Translation docstrings with gettext

Lie Ryan lie.1296 at
Mon Apr 5 10:27:07 EDT 2010

On 04/05/10 20:31, sapient wrote:
> Hello.
> I found several discussions where this question was asked, but was not
> answered.

Why would you want to translate docstring? Docstring is meant for
developers not users. 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

Anyway, my job is to answer questions, not question the economic
feasibility of your decision, so try this:

__doc__ = _("""testmodule docstring""")

class TestClass:
    __doc__ = _("""testmodule.TestClass docstring""")

    def testClassMethod(self):
        __doc__ = _("""testmodule.TestClass.testClassMethod docstring""")
        print _("Call TestClass.testClassMethod()")

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.

More information about the Python-list mailing list