Skip to content

target_language not passed to the translate method in certain cases #369

Closed

Description

@ale-rt

I have a template where I define i18n:target.
That adds to the local variables a variable target_language.
That variable is not passed when we are going to translate the messages in many cases.

I did a stupid test applying this patch:

[ale@flo chameleon]$ git diff
diff --git a/src/chameleon/i18n.py b/src/chameleon/i18n.py
index 449cda1..bb85734 100644
--- a/src/chameleon/i18n.py
+++ b/src/chameleon/i18n.py
@@ -82,6 +82,7 @@ else:   # pragma: no cover
 
 def simple_translate(msgid, domain=None, mapping=None, context=None,
                      target_language=None, default=None):
+    print(f"simple_translate called with msgid={msgid} target_language={target_language}")
     if default is None:
         default = getattr(msgid, "default", msgid)

and tried to render this template:

<html i18n:domain="bar"
      i18n:target="string:test"
>
  target_language: ${target_language}

  ${foo}

  <tal:foo replace="structure foo" />

  <tal:foo replace="structure foo"
           i18n:translate=""
  />

</html>

with this code:

from chameleon import PageTemplate
from zope.i18n import Message

foo = Message("foo", default="Foo", domain="bar")

with open("b.pt") as f:
    pt = PageTemplate(f.read())
    print(pt(foo=foo))

The output is:

[ale@flo chameleon]$ ./py3/bin/python test.py 
simple_translate called with msgid=foo target_language=None
simple_translate called with msgid=foo target_language=None
simple_translate called with msgid=foo target_language=test
<html
>
  target_language: test

  Foo

  Foo

  Foo

</html>

As you can see:

  1. the can render the proper value for target_language
  2. ${foo} is processed passing target_language=None
  3. <tal:foo replace="structure foo" /> is processed passing target_language=None
  4. Only if I add i18n:translate="" (which I would not expect to need), the target_language is properly passed.

Activity

  1. malthe commented on Nov 15, 2023

    @malthe
    Owner

    This should be fixed now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions