Translation docstrings with gettext
Peter Otten
__peter__ at web.de
Thu Apr 8 07:26:37 EDT 2010
sapient wrote:
> I found several discussions where this question was asked, but was not
> answered.
>
> 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 pygettext.py has key --docstrings that forces extraction
> docstrings from module, so I suppose, that it must be way to use thier
> translations.
> So, question is: How to translate docstrings in my example?
You could leave the docstrings alone and monkey-patch inspect.getdoc() to do
the translation when help() is invoked:
#! /usr/bin/python2.6
# -*- coding: utf-8 -*-
import os, gettext
localedir = os.path.join( os.path.dirname(__file__), "locale/" )
t = gettext.translation( 'testmodule', localedir=localedir,
languages=['ru'], codeset="utf-8" )
t.install()
import testmodule
import inspect
def getdoc(object):
try:
doc = object.__doc__
except AttributeError:
return None
if not isinstance(doc, basestring):
return None
return inspect.cleandoc(_(doc))
inspect.getdoc = getdoc
help( testmodule )
testmodule.TestClass().testClassMethod()
Peter
More information about the Python-list
mailing list