Friday, May 30, 2014

Re: django ugettext translation glitch or as designed?

Sorry for the large text... I forget to change it - I guess I am going blind :-)

On Friday, May 30, 2014 6:51:35 PM UTC-7, visionary800 wrote:
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/ad352064-6e11-4f0c-89a6-a0fab2d091c5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment