Search code examples
djangotranslationgettext

weird django translations behaviour


I have weird problem with django translations that i need help figuring out. Problem is that instead of getting translated string every time i seem to get randomly either translated string or default string.

I have created a class for putting "action" buttons on several pages. Many of those buttons are reusable so why should i put

<a href="something">blabla</a>

into several templates when i can create simple toolbar and use

toolbar.add(tool)

in view and then just use templatetag for rendering all the tools.... anyway.

This is example of one tool:

class NewUserGroupButton(MyTool):
    tool_id = 'new-user-group'
    button_label = ugettext(u'Create new user group')
    tool_href = 'new/'
    js = ()

    def render(self):
        s = '<a id="%(tool_id)s" href="%(tool_href)s" class="button blue">%(button_label)s</a>'
        return s % {'tool_id': self.tool_id, 'tool_href': self.tool_href, 'button_label':self.button_label}

The tools are rendered like this:

class ToolbarTools:
    def __init__(self):
        self.tools = []

    def add(self, tool):
        self.tools.append(tool)

    def render(self):
        # Organize tools by weight
        self.tools.sort(key=lambda x: x.weight)

        # Render tools
        output = '<ul id="toolbar" class="clearfix">'

        for tool in self.tools:
            output += '<li id="%s">' % ('tool-'+ tool.tool_id)
            output += tool.render() if tool.renderable else ''
            output += '</li>'

        output += '</ul>'

        # Reset tools container
        self.tools = []

        return mark_safe(output)

im using ugettext to translate the string. using (u)gettext=lambda s:s or ugettext_lazy gives me either no translations or proxy object corresponding to function choices.

And like i said - while rest of the page is in correct language the toolbar buttons seem to be randomly either translated or not. The faulty behaviour remains consistent if i move between different pages with different "tools" or even refresh the same page several times.

Could someone please help me to figure out what is causing this problem and how to fix it?


Solution

  • Problem solved.

    The issue itself (random translating) was perhaps caused by using ugettext. But at the same time i should have used ugettext_lazy instead.

    And thus the problem really came from ugettext_lazy returning proxy object not translated string... and that is caused by this:

    [Quote] Working with lazy translation objects The result of a ugettext_lazy() call can be used wherever you would use a unicode string (an object with type unicode) in Python. If you try to use it where a bytestring (a str object) is expected, things will not work as expected, since a ugettext_lazy() object doesn't know how to convert itself to a bytestring. You can't use a unicode string inside a bytestring, either, so this is consistent with normal Python behavior. For example:

    This is fine: putting a unicode proxy into a unicode string.
    u"Hello %s" % ugettext_lazy("people")
    This will not work, since you cannot insert a unicode object
    into a bytestring (nor can you insert our unicode proxy there)
    "Hello %s" % ugettext_lazy("people")
    

    [/Quote]

    taken from here: https://docs.djangoproject.com/en/dev/topics/i18n/translation/#working-with-lazy-translation-objects

    Alan