translation of view messages and business related messages not working

32 views
Skip to first unread message

Marcela Campo

unread,
Oct 30, 2014, 7:45:32 PM10/30/14
to django...@googlegroups.com
Hi,
  I am using the translation functionality in Django 1.7 successfully for plain strings in a template, so something simple like

{% trans "Edit Client" %}

works just fine.


I am now trying to translate success_message from views with the SuccessMessageMixin and also error messages that bubble up from business logic but I just can't make those work.The messages show up for translation in the .po file. For example:

# python-format
#: views/adminDashboard.py:158
#, python-format
msgid "Productive Unit %(name)s has been created."
msgstr "La Unidad Productiva %(name)s ha sido creada."

I have compiled the messages already.

In my view

....
....
from django.utils.translation import ugettext as _
class CustomerFieldUpdateView(AdminRequiredMixin, SuccessMessageMixin, UpdateView):
    model = Field
    template_name = 'ui/field_update_form.html'
    success_message = _('Productive Unit %(name)s has been updated.')
.....

In the template:
....
{% load i18n %}
....
{% if messages %}
    {% for message in messages %}
              {{ message }}
    {% endfor %}
{% endif %}
....


If I look at self.request.LANGUAGE_CODE in the view is set to 'es' but still the messages that bubble up from python code are displayed in the default ('en') whereas the plain strings in the template are correctly translated to spanish. I also tried using {% trans message.message %} or blocktrans as well, but nothing works.

Any ideas what I am missing?

Thanks!!!
Marcela





Ramiro Morales

unread,
Oct 30, 2014, 9:39:56 PM10/30/14
to django...@googlegroups.com


On Oct 30, 2014 8:45 PM, "Marcela Campo" <marcel...@gmail.com> wrote:
>
> Hi,
>   I am using the translation functionality in Django 1.7 successfully for plain strings in a template, so something simple like
>
> {% trans "Edit Client" %}
>
> works just fine.
>
>
> I am now trying to translate success_message from views with the SuccessMessageMixin and also error messages that bubble up from business logic but I just can't make those work.The messages show up for translation in the .po file. For example:
>
> # python-format
> #: views/adminDashboard.py:158
> #, python-format
> msgid "Productive Unit %(name)s has been created."
> msgstr "La Unidad Productiva %(name)s ha sido creada."
>
> I have compiled the messages already.
>
> In my view
>
> ....
> ....
> from django.utils.translation import ugettext as _

Try replacing ugettext with ugettext_lazy. Read the relevante documentation for the details.

> class CustomerFieldUpdateView(AdminRequiredMixin, SuccessMessageMixin, UpdateView):
>     model = Field
>     template_name = 'ui/field_update_form.html'
>     success_message = _('Productive Unit %(name)s has been updated.')
> .....
>
> In the template:
> ....
> {% load i18n %}
> ....
> {% if messages %}
>     {% for message in messages %}
>               {{ message }}
>     {% endfor %}
> {% endif %}
> ....
>
>
> If I look at self.request.LANGUAGE_CODE in the view is set to 'es' but still the messages that bubble up from python code are displayed in the default ('en') whereas the plain strings in the template are correctly translated to spanish. I also tried using {% trans message.message %} or blocktrans as well, but nothing works.
>
> Any ideas what I am missing?
>
> Thanks!!!
> Marcela
>
>
>
>
>

> --
> 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...@googlegroups.com.
> To post to this group, send email to django...@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/be1ce953-ccee-4016-b4d6-686655755986%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Marcela Campo

unread,
Oct 31, 2014, 7:58:19 AM10/31/14
to django...@googlegroups.com
Thanks Ramiro, that worked.
 I think I understand why, but there's one thing I still don't get. I have some error messages that I was marking for translation with  ugettext_noop, as I read it was a good way if I wanted for example to log the message without translation and translate it only when displaying to the user. They appeared in the .po file, translated and compile the messages but I couldn't find the way to get them to display in other language either. I first thought it was because they were flagged as fuzzy. Removed that, recompiled and still no luck. I changed to  ugettext_lazy and they are working now too, but for what understand with  ugettext_lazy it will get translated when I want to use it, so logging would get screwed.
What would the correct way to make the noop to work? I went over the documentation many times already but I just don't get what I am missing.

Thanks
Marcela
Reply all
Reply to author
Forward
0 new messages