Friday, May 30, 2014

Re: django ugettext translation glitch or as designed?

On Fri, May 30, 2014 at 10:51 PM, visionary800 <drjosereynoso@gmail.com> wrote:
> Bad: 2 - This Fails: when ugettext is in another file
> file : /project/app/views.py
>
> ...other imports
> from .mytext import FROM_ANOTHER_FILE
>
> class BaseView(ContextMixin, View):
> template = 'base.html'
>
> def get(self, request, *args, **kwargs):
> context = self.get_context_data()
> return render(request, self.template, context)
>
> def get_context_data(self, **kwargs):
> ....
> context['translate_this']= FROM_ANOTHER_FILE
> return context
>
> file: /project/app/text.py
> from django.utils.translation import ugettext
>
> FROM_ANOTHER_FILE = ugettext('name')
>
> -------------------------------------------------------------------------
>
> The primary difference between the two examples is that when I separate
> ugettext() to another file - it fails.
> The result is that 'name' never converts to 'nombre' when changing the
> language.
> It works fine in example 1, 'name' and 'nombre' change depending on language
> selected but not in example 2.

In example 2 your translatable literal is defined and marked-up with
the translation function at the global scope of the module. In these
cases you need to use lazy translations, i.e. ugettext_lazy() instead
of ugettext().

Read the the relevant documentation carefully for the details:

https://docs.djangoproject.com/en/1.6/topics/i18n/translation/#lazy-translation

HTH,

--
Ramiro Morales
@ramiromorales

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAO7PdF9uu7BF%2Bf5DK5kXa%2B5NeSQmne89N2x%3DGEwxLckQYyTBig%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment