Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lazy_gettext doesn't use current locale settings #84

Open
GoogleCodeExporter opened this issue May 5, 2015 · 1 comment
Open

lazy_gettext doesn't use current locale settings #84

GoogleCodeExporter opened this issue May 5, 2015 · 1 comment

Comments

@GoogleCodeExporter
Copy link


What steps will reproduce the problem?

1.
Create a new form using wtforms, e.g.:

import wtforms
from webapp2_extras.i18n import lazy_gettext as _

class TetForm(wtforms.Form):
    first_name = wtforms.TextField(_('First name'))

This uses lazy_gettext, so that it will not execute gettext on "First name" on 
module load, but is supposed to execute it each time the string "First name" is 
used.

Now init this form in a test request handler and set its route to /test/:

class TestHandler(webapp2.RequestHandler):

    def render_template(self, template, context):
        """
        Renders the template using jinja2
        """
        ...

    def get(self):
        locale = self.request.get('locale')
        i18n_obj = i18n.get_i18n()
        i18n_obj.set_locale(locale)
        form = TestForm()
        self.render_template('form.html', {'form': form})


Use the following jinja2 template (form.html):
<h1>{{ gettext("Form") }}</h1>
<form action='/test/' method="post">
{{ form.first_name }}
</form>

2.
Create translations for two languages for the string "First name" using gettext.

Let's say:

locale/de_DE/LC_MESSAGES/messages.po for german version:
"Form" = "Formular"
"First name" = "Vorname"

locale/sl_SI/LC_MESSAGES/messages.po for slovenian version:
"Form" = "Obrazec"
"First name" = "Ime"

Compile them.

3.

Open the following url:
/test/?locale=sl_SI
this one will show "Obrazec" in h1 and "Ime" as input label, both strings in 
slovenian, which corresponds to the locale setting sl_SI.

And now go to:
/test/?locale=de_DE

What is the expected output? What do you see instead?
Now it will show "Formular" (german) in h1 and input label will show "Ime" 
(slovenian) instead of "Vorname" (german), although we set the locale setting 
on this request to de_DE.


What version of the product are you using? On what operating system?
webapp2 2.5.2

Please provide any additional information below.

After looking into it, the issue is clearly related to the fact that Babel's 
LazyProxy class is caching the value - babel.support.py:

def value(self):
    if self._value is None:
        value = self._func(*self._args, **self._kwargs)
        object.__setattr__(self, '_value', value)
    return self._value
value = property(value)

and i18n.lazy_gettext is using LazyProxy for lazy execution of the non-lazy 
i18n.gettext, but it only gets executed once in the lifetime of the app 
instace, instead of once per request.

Original issue reported on code.google.com by [email protected] on 18 Sep 2013 at 11:37

@GoogleCodeExporter
Copy link
Author

here's a rough workaround -- effectively disabling the caching of translations. 
I've not checked out the performance hit, but it certainly beats having text 
coming out in every language imaginable:


from babel.support import LazyProxy
from webapp2_extras import i18n

class LazierProxy(LazyProxy):
    def value(self):
        return self._func(*self._args, **self._kwargs)
    value = property(value)

def lazy_gettext(string, **variables):
    '''
    https://code.google.com/p/webapp-improved/issues/detail?id=84
    '''
    return LazierProxy(i18n.gettext, string, **variables)

[then import lazy_gettext from here rather than webapp2_extras.i18n]

Original comment by [email protected] on 17 Feb 2014 at 10:56

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

No branches or pull requests

1 participant