Saturday, October 31, 2015

Re: Translation is not working for Template even though po and mo files are created

The translation for my template finally gets to work after I explicitly have LOCALE_PATHS configured in settings.py!!!!!
Thanks very much for providing the hint :)

Actually the models and template come from different applications:

~/django-swingtime/django-swingtime-master/swingtime # Models come from this swingtime application where translation worked.

~/django-swingtime/django-swingtime-master/demo # Templates come from this demo application where translation didn't work previously. (I'm running the server from demo folder actually... '~/django-swingtime/django-swingtime-master/demo> python manage.py runserver
')
It's weird demo was not recognized while swingtime was recognized for translation, though.

Br
Sean

On Friday, October 30, 2015 at 9:47:53 PM UTC+8, Andréas Kühne wrote:
Hmmm....

I'm a bit at a loss here. But does Django know where to find the .mo files? Is it only your template that isn't getting translated, because you said that the form itself is?

Regards,

Andréas

2015-10-30 12:45 GMT+01:00 Sean Xu <seanx...@gmail.com>:
Now I have corrected the orders of each Middleware class and removed the duplicated SessionMiddleware:
MIDDLEWARE_CLASSES = (
   
'django.contrib.sessions.middleware.SessionMiddleware',
   
'django.middleware.locale.LocaleMiddleware',
   
'django.middleware.common.CommonMiddleware',    
   
'django.contrib.auth.middleware.AuthenticationMiddleware',    
   
'django.contrib.messages.middleware.MessageMiddleware',
)
I also added django.core.context_processors.i18n as one of the context processors. (I just realized that I'm using Django version 1.7 something so django.template.context_processors.i18n should really be django.core.context_processors.i18n).
But unfortunately the translation for my template still does not work :(

On Friday, October 30, 2015 at 7:27:36 PM UTC+8, Sean Xu wrote:
Hi Andréas,

Thanks very much for providing the help.
Currently the middleware classes are configured like this:

MIDDLEWARE_CLASSES = (
   
'django.middleware.common.CommonMiddleware',
   
'django.contrib.sessions.middleware.SessionMiddleware',
   
'django.contrib.auth.middleware.AuthenticationMiddleware',
   
'django.contrib.sessions.middleware.SessionMiddleware',
   
'django.middleware.locale.LocaleMiddleware',
   
'django.contrib.messages.middleware.MessageMiddleware',
)

 I guess the order of these middleware is not correct because LocaleMiddleware should come before CommonMiddleware.

I have LANGUAGES defined in settings.py like this:
from django.utils.translation import ugettext_lazy as _

...
LANGUAGES
= (
 
('zh', _('Chinese')),
 
('en', _('English')),
)
And I don't have django.template.context_processors.i18n defined for TEMPLATE_CONTEXT_PROCESSORS in settings.py. Do I need to add this i18n context processor to settings.py?

Br
Sean

On Friday, October 30, 2015 at 6:07:21 PM UTC+8, Andréas Kühne wrote:
Hi,

Have you made sure that you have activated the languages you want in your application? 
Also, how is the current language being selected?

Regards,

Andréas

2015-10-30 7:45 GMT+01:00 Sean Xu <seanx...@gmail.com>:
Hi,

I'm learning Django Translation using django-swingtime with Django 1.7.9 installed.
I followed the official docs and embedded my string to translate with <title>{% trans "String to translate" %}</title> in my Template and managed to create both the po and mo files successfully.
But, the page is still shown up in English.
Note: the translation works just fine for my models.

Partly of the template I was using can be found from below:
{% extends "base.html" %}
{% load url from future %}
{% load i18n %}
{% block title %}Event Occurrence{% endblock %}
{% block main_content %}
     
<h3>{% trans "Swingtime Event Occurrence" %}</h3>
     
<h4>
         
<a href="{{ occurrence.event.get_absolute_url }}">{{ occurrence.title }}</a>
         
&ndash;
         
{% with occurrence.start_time as st  %}
         
<a href="{% url 'swingtime-daily-view' st.year st.month st.day %}">
           
{{ st|date:"l, F jS P" }}</a>
       
</h4>
        {% endwith %}
     <dl>
         <dt>{% trans "Event type:" %}</
dt>
         
<dd>{{ occurrence.event.event_type }}</dd>

The subroutine to render my Template is:
#-------------------------------------------------------------------------------
def occurrence_view(
    request
,
    event_pk
,
    pk
,
   
template='swingtime/occurrence_detail.html',
    form_class
=forms.SingleOccurrenceForm
):
   
'''
    View a specific occurrence and optionally handle any updates.
   
    Context parameters:
   
    ``occurrence``
        the occurrence object keyed by ``pk``


    ``form``
        a form object for updating the occurrence
    '''

 
    occurrence
= get_object_or_404(Occurrence, pk=pk, event__pk=event_pk)
   
if request.method == 'POST':
        form
= form_class(request.POST, instance=occurrence)
       
if form.is_valid():
            form
.save()
           
return http.HttpResponseRedirect(request.path)
   
else:  
        form
= form_class(instance=occurrence)
       
   
return render(request, template, {'occurrence': occurrence, 'form': form})

And the url configuration associated with the Template reads:
    url(
        r
'^events/(\d+)/(\d+)/$',
        views
.occurrence_view,
        name
='swingtime-occurrence'
   
),

Could some one help me solve this problem?
Thanks very much

Br
Sean

--
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/a45afc10-97ad-4574-b541-14ff67e892cd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
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/4c2d6bbe-c586-410c-8c61-338ecc01caf1%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
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/a3d0f50f-a5dd-4d84-915d-3b31266eea9c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment