wichert / lingua

Translation toolkit for Python
Other
46 stars 32 forks source link

issue with using babel-mako extractor? #44

Closed tisdall closed 10 years ago

tisdall commented 10 years ago

After updating to 2.5... (though it also didn't work in 2.4, but gave a different error)

I have a file lingua.cfg with contents:

[extension:.mako]
plugin = babel-mako

[extension:.pt]
plugin = xml

[extension:.py]
plugin = python

Then I try to use pot-create:

$env/bin/pot-create -c lingua.cfg -d mainserver -o mainserver/locale/mainserver.pot mainserver
Traceback (most recent call last):
  File "env/bin/pot-create", line 11, in <module>
    sys.exit(main())
  File "/sites/metrics_dev/env/lib/python3.4/site-packages/lingua/extract.py", line 218, in main
    read_config(options.config)
  File "/sites/metrics_dev/env/lib/python3.4/site-packages/lingua/extract.py", line 154, in read_config
    EXTENSIONS[extension] = EXTRACTORS[plugin](config)
TypeError: wrapper() missing 1 required positional argument: 'options'

prior to the update I got:

$env/bin/pot-create -c lingua.cfg -d mainserver -o mainserver/locale/mainserver.pot mainserver
Traceback (most recent call last):
  File "env/bin/pot-create", line 11, in <module>
    sys.exit(main())
  File "/sites/metrics_dev/env/lib/python3.4/site-packages/lingua/extract.py", line 250, in main
    catalog.save(options.output)
  File "/sites/metrics_dev/env/lib/python3.4/site-packages/polib.py", line 424, in save
    contents = getattr(self, repr_method)()
  File "/sites/metrics_dev/env/lib/python3.4/site-packages/polib.py", line 620, in __unicode__
    return ret + _BaseFile.__unicode__(self)
  File "/sites/metrics_dev/env/lib/python3.4/site-packages/polib.py", line 320, in __unicode__
    ret.append(entry.__unicode__(self.wrapwidth))
  File "/sites/metrics_dev/env/lib/python3.4/site-packages/polib.py", line 956, in __unicode__
    val = getattr(self, c[0])
  File "/sites/metrics_dev/env/lib/python3.4/site-packages/lingua/extract.py", line 45, in comment
    return u'\n'.join(self._comments)
TypeError: sequence item 0: expected str instance, list found
wichert commented 10 years ago

Looks like 2.5 broke the Babel plugin handling :(. I'll look into that tomorrow.

tisdall commented 10 years ago

The exception I got on 2.4, was that something I was doing wrong or something that's been fixed in 2.5?

wichert commented 10 years ago

I am guessing the Babel plugin API uses strings for comments instead of a list of strings, so the proxy-code in lingua needs to be modified to convert that. So lingua bug, not your fault.

wichert commented 10 years ago

Can you check if the current code in master works for you? Please note that the format of the configuration file has changed a bit.

tisdall commented 10 years ago
# env/bin/pot-create -c lingua.cfg -o mainserver/locale/mainserver.pot mainserver
Traceback (most recent call last):
  File "env/bin/pot-create", line 9, in <module>
    load_entry_point('lingua==2.6dev', 'console_scripts', 'pot-create')()
  File "/sites/repos/lingua/src/lingua/extract.py", line 253, in main
    for message in extractor(real_filename, options):
  File "/sites/repos/lingua/src/lingua/extractors/babel.py", line 28, in __call__
    for (lineno, _, msgid, comment) in self.extractor(fileobj, DEFAULT_KEYWORDS.keys(), (), {}):
TypeError: extract() takes 4 positional arguments but 5 were given

lingua.cfg:

[extensions]
.mako = babel-mako
.pt = chameleon
.py = python
wichert commented 10 years ago

Does that change help?

tisdall commented 10 years ago
# env/bin/pot-create -c lingua.cfg -o mainserver/locale/mainserver.pot mainserver
Traceback (most recent call last):
  File "env/bin/pot-create", line 9, in <module>
    load_entry_point('lingua==2.6dev', 'console_scripts', 'pot-create')()
  File "/sites/repos/lingua/src/lingua/extract.py", line 268, in main
    catalog.save(options.output)
  File "/sites/metrics_dev/env/lib/python3.4/site-packages/polib.py", line 424, in save
    contents = getattr(self, repr_method)()
  File "/sites/metrics_dev/env/lib/python3.4/site-packages/polib.py", line 620, in __unicode__
    return ret + _BaseFile.__unicode__(self)
  File "/sites/metrics_dev/env/lib/python3.4/site-packages/polib.py", line 320, in __unicode__
    ret.append(entry.__unicode__(self.wrapwidth))
  File "/sites/metrics_dev/env/lib/python3.4/site-packages/polib.py", line 956, in __unicode__
    val = getattr(self, c[0])
  File "/sites/repos/lingua/src/lingua/extract.py", line 47, in tcomment
    return u'\n'.join(self._tcomments)
TypeError: sequence item 0: expected str instance, NoneType found
wichert commented 10 years ago

Can you provide a sample mako file which triggers this error?

tisdall commented 10 years ago

I just remembered I only have one file that I started adding translation markup into:

<%inherit file='layout.mako' />

<div class="col-md-12" id="maincontent">

% if failed_attempt:
<p><font color="red">${_('Invalid credentials, try again.')}</font></p>
% endif
<form method="post" action="${ request.route_path('login') }">
    <p>
        <label for="login">${_('Login')}</label><br>
        <input type="text" name="login" value="${ login }" autocomplete="off">
    </p>
    <p>
        <label for="passwd">${_('Password')}</label><br>
        <input type="password" name="passwd" autocomplete="off">
    </p>
    <input type="hidden" name="next" value="${ next }">
    <input type="submit" name="submit">
</form>

</div>

(Please excuse the font tag... ^_^ I think this was cut-n-copied from an example.)

wichert commented 10 years ago

I pushed a few changes which makes the Babel-python plugin work for me. Does that fix your problems as well?

tisdall commented 10 years ago

Success! Thank you!

tisdall commented 10 years ago

... nearly. :) I tried adding comments as shown here: http://docs.makotemplates.org/en/latest/usage.html#babel I don't get any errors, but the comments aren't added to the POT file.

<%inherit file='layout.mako' />

<div class="col-md-12" id="maincontent">

% if failed_attempt:
<p><font color="red">
  ## TRANSLATORS: This is a proper name. See the gettext
  ## manual, section Names.
  ${_('Invalid credentials, try again.')}
</font></p>
% endif
<form method="post" action="${ request.route_path('login') }">
    <p>
        <label for="login">${_('Login')}</label><br>
        <input type="text" name="login" value="${ login }" autocomplete="off">
    </p>
    <p>
        <label for="passwd">${_('Password')}</label><br>
        <input type="password" name="passwd" autocomplete="off">
    </p>
    <input type="hidden" name="next" value="${ next }">
    <input type="submit" name="submit">
</form>

</div>
wichert commented 10 years ago

That sounds like a separate issue. The problem here is that you need to explicitly tell the mako plugin how to identify comments. With Babel's extract_messages you do that with the -c option. With lingua you can do that (since the last change) through the configuration file:

[extensions]
.mako = babel-mako

[extractor:babel-mako]
comment-tags = TRANSLATORS:
tisdall commented 10 years ago

Thanks for all the help!

wichert commented 9 years ago

FYI: there is a pull request for a mako lingua plugin now.