django ugettext translation glitch or as designed?

42 views
Skip to first unread message

visionary800

unread,
May 30, 2014, 9:51:35 PM5/30/14
to django...@googlegroups.com
I am running into a challenge.  For both examples below everything is good except when I move the ugettext() to a separate file.
Good: After running the django-admin.py makemessages -l es, both examples produce the appropriate .po file.
Good: I change ../locale/es/LC_MESSAGES/django.po
          #: nav/views.py: 
          msgid "name" 
         msgstr "nombre"
Good: django-admin.py compilemessages
Good: I get the appropriate file, ../locale/es/LC_MESSAGES/django.mo 

This is a simplified version of what is happening.

-------------------------------------------------------------------------------
Good: 1 - This works: when ugettext is within the view.
file : /project/app/views.py

...other imports
from django.utils.translation import ugettext

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']= ugettext('name')
       return context
-----------------------------------------------------------------------------

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.

visionary800

unread,
May 30, 2014, 9:56:43 PM5/30/14
to django...@googlegroups.com
Sorry for the large text... I forget to change it - I guess I am going blind :-)

Ramiro Morales

unread,
May 30, 2014, 10:39:51 PM5/30/14
to django...@googlegroups.com
On Fri, May 30, 2014 at 10:51 PM, visionary800 <drjose...@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

visionary800

unread,
May 30, 2014, 11:24:58 PM5/30/14
to django...@googlegroups.com
Thank you Ramiro!  That was it - I changed to ugettext_lazy() and it worked!

I want to separate the text from the templates - this is why I want to place the text in a separate file.  Placing the text within the template is a d

What approach should I take to just separate the text from from the templates and views?  I want the text easy to edit without worrying about tags around the text.  Any suggestions? 
Reply all
Reply to author
Forward
0 new messages