Translation docstrings with gettext

Aleksey Rezvov <[email protected]> Mon, 5 Apr 2010 15:03:23 +0400
Newsgroups gmane.comp.python.internationalization
Message-ID <[email protected]>
--===============1934033319==
Content-Type: multipart/alternative; boundary=00032555b90e0d8b1604837b4702

--00032555b90e0d8b1604837b4702
Content-Type: text/plain; charset=UTF-8

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...<http://www.google.com/url?sa=D&q=https://docs.google.com/leaf%3Fid%3D0B_rE4w6PFDYWODg5ZWJlYjMtYTQ5ZS00MTE3LTgxOWUtNjc5NzEzNzVjYzdh%26hl%3Den&usg=AFQjCNENWMKxF2w-YqNHLoEtwCCNYTb8lg>
So, question is: How to translate docstrings in my example?

--00032555b90e0d8b1604837b4702
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<font class=3D"fixed_width" face=3D"Courier, Monospaced">Hello. <br><br>I a=
lready post this in comp.lang.python, but after found this place, </font><f=
ont class=3D"fixed_width" face=3D"Courier, Monospaced">I think it is more=
=20
appropriate</font><font class=3D"fixed_width" face=3D"Courier, Monospaced">=
. Sorry for duplication.<br> </font><p><font class=3D"fixed_width" face=3D"=
Courier, Monospaced">I found several=20
discussions where this question was asked, but was not <br> answered. <br>
 </font></p><p><font class=3D"fixed_width" face=3D"Courier, Monospaced">Now=
 I
 am creating Python-API for my application, and want create it <br> with
 translation support, including documentation strings for modules, <br>=20
classes, methods etc. <br> </font></p><p><font class=3D"fixed_width" face=
=3D"Courier, Monospaced">It is simple to translate special-marked=20
strings with gettext, but it <br> is problem with docstrings: if I mark=20
them for translation like <br> _(&quot;&quot;&quot;Some documentation strin=
g&quot;&quot;&quot;) then=20
it is not recognized as <br> docstring. If I omit _() markup, then=20
string is not translated. <br> </font></p><p><font class=3D"fixed_width" fa=
ce=3D"Courier, Monospaced">Script pygettext.py has key --docstrings that
 forces extraction <br> docstrings from module, so I suppose, that it=20
must be way to use thier <br> translations. <br> </font></p><p><font class=
=3D"fixed_width" face=3D"Courier, Monospaced">--- <br> I create small=20
example, that demonstrates this problem: <br> Module with docstrings for
 translation: <br> {{{ <br> #!python <br> &quot;&quot;&quot;testmodule docs=
tring&quot;&quot;&quot; <br>
 </font></p><p><font class=3D"fixed_width" face=3D"Courier, Monospaced">cla=
ss
 TestClass: <br> =C2=A0 =C2=A0 &quot;&quot;&quot;testmodule.TestClass docst=
ring&quot;&quot;&quot; <br> </font></p><p><font class=3D"fixed_width" face=
=3D"Courier, Monospaced">=C2=A0 =C2=A0 def=20
testClassMethod(self): <br> =C2=A0 =C2=A0 =C2=A0 =C2=A0=20
&quot;&quot;&quot;testmodule.TestClass.testClassMethod docstring&quot;&quot=
;&quot; <br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 print=20
_(&quot;Call TestClass.testClassMethod()&quot;) <br> </font></p><div id=3D"=
qhide_541645" style=3D"display: block;" class=3D"qt"><font class=3D"fixed_w=
idth" face=3D"Courier, Monospaced">}}} <br> <br></font></div><font class=3D=
"fixed_width" face=3D"Courier, Monospaced">Script for testing=20
translation: <br> {{{ <br> #!python <br> import os, gettext <br> </font><p>=
<font class=3D"fixed_width" face=3D"Courier, Monospaced">localedir =3D=20
os.path.join( os.path.dirname(__file__), &quot;locale/&quot; ) <br> </font>=
</p><p><font class=3D"fixed_width" face=3D"Courier, Monospaced">t =3D gette=
xt.translation(
 &#39;testmodule&#39;, localedir=3Dlocaledir, <br> languages=3D[&#39;ru&#39=
;],=20
codeset=3D&quot;cp1251&quot; ) <br> t.install() <br> </font></p><p><font cl=
ass=3D"fixed_width" face=3D"Courier, Monospaced">import testmodule <br> </f=
ont></p><p><font class=3D"fixed_width" face=3D"Courier, Monospaced">help( t=
estmodule ) <br>
=20
testmodule.TestClass().testClassMethod() <br> </font></p><div id=3D"qhide_5=
41646" style=3D"display: block;" class=3D"qt"><font class=3D"fixed_width" f=
ace=3D"Courier, Monospaced">}}} <br> <br></font></div><font class=3D"fixed_=
width" face=3D"Courier, Monospaced">It successfully=20
translates _(&quot;Call TestClass.testClassMethod()&quot;) but <br> all doc=
strings
 stay untranslated. <br> Full example exists here: <br> <a target=3D"_blank=
" rel=3D"nofollow" href=3D"http://www.google.com/url?sa=3DD&amp;q=3Dhttps:/=
/docs.google.com/leaf%3Fid%3D0B_rE4w6PFDYWODg5ZWJlYjMtYTQ5ZS00MTE3LTgxOWUtN=
jc5NzEzNzVjYzdh%26hl%3Den&amp;usg=3DAFQjCNENWMKxF2w-YqNHLoEtwCCNYTb8lg">htt=
ps://docs.google.com/leaf?id=3D0B_rE4w6PFDYWODg5ZWJlYjMtYTQ5ZS00MTE3...</a>
 <br> </font><font class=3D"fixed_width" face=3D"Courier, Monospaced">So,
 question is: How to translate docstrings in my example? </font>

--00032555b90e0d8b1604837b4702--

--===============1934033319==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
I18n-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/i18n-sig

--===============1934033319==--