Friday, May 30, 2014

django ugettext translation glitch or as designed?

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.

--
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/2302f88c-2050-4265-bf22-6e4d34a2b486%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment