Translation docstrings with gettext

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view

Translation docstrings with gettext


I already post this in comp.lang.python, but after found this place,
I think it is more appropriate. Sorry for duplication.

I found several discussions where this question was asked, but was not

Now I am creating Python-API for my application, and want create it
with translation support, including documentation strings for modules,
classes, methods etc.

It is simple to translate special-marked strings with gettext, but it
is problem with docstrings: if I mark them for translation like
_("""Some documentation string""") then it is not recognized as
docstring. If I omit _() markup, then string is not translated.

Script has key --docstrings that forces extraction
docstrings from module, so I suppose, that it must be way to use thier

I create small example, that demonstrates this problem:
Module with docstrings for translation:
"""testmodule docstring"""

class TestClass:
    """testmodule.TestClass docstring"""

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


Script for testing translation:
import os, gettext

localedir = os.path.join( os.path.dirname(__file__), "locale/" )

t = gettext.translation( 'testmodule', localedir=localedir,
languages=['ru'], codeset="cp1251" )

import testmodule

help( testmodule )


It successfully translates _("Call TestClass.testClassMethod()") but
all docstrings stay untranslated.
Full example exists here:
So, question is: How to translate docstrings in my example?
I18n-sig mailing list
[hidden email]
Reply | Threaded
Open this post in threaded view

Re: Translation docstrings with gettext

"Martin v. Löwis"

> So, question is: How to translate docstrings in my example?

You need to modify the help() function, to not directly render the
docstring, but to first pass it through gettext. It might be best to
replace inspect.cleandoc for that.


I18n-sig mailing list
[hidden email]