From aleksey.rezvov at gmail.com Mon Apr 5 13:03:23 2010 From: aleksey.rezvov at gmail.com (Aleksey Rezvov) Date: Mon, 5 Apr 2010 15:03:23 +0400 Subject: [I18n-sig] Translation docstrings with gettext Message-ID: Hello. 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 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. --- I create small example, that demonstrates this problem: Module with docstrings for translation: {{{ #!python """testmodule docstring""" class TestClass: """testmodule.TestClass docstring""" def testClassMethod(self): """testmodule.TestClass.testClassMethod docstring""" print _("Call TestClass.testClassMethod()") }}} Script for testing translation: {{{ #!python import os, gettext localedir = os.path.join( os.path.dirname(__file__), "locale/" ) t = gettext.translation( 'testmodule', localedir=localedir, languages=['ru'], codeset="cp1251" ) t.install() import testmodule help( testmodule ) testmodule.TestClass().testClassMethod() }}} It successfully translates _("Call TestClass.testClassMethod()") but all docstrings stay untranslated. Full example exists here: https://docs.google.com/leaf?id=0B_rE4w6PFDYWODg5ZWJlYjMtYTQ5ZS00MTE3... So, question is: How to translate docstrings in my example? -------------- next part -------------- An HTML attachment was scrubbed... URL: From martin at v.loewis.de Wed Apr 7 16:23:48 2010 From: martin at v.loewis.de (=?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?=) Date: Wed, 07 Apr 2010 16:23:48 +0200 Subject: [I18n-sig] Translation docstrings with gettext In-Reply-To: References: Message-ID: <4BBC9574.4070006@v.loewis.de> > 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. Regards, Martin