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

804 views
Skip to first unread message

Sean Xu

unread,
Oct 30, 2015, 5:58:18 AM10/30/15
to Django users
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

Andreas Kuhne

unread,
Oct 30, 2015, 6:07:21 AM10/30/15
to django...@googlegroups.com
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

--
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.

Sean Xu

unread,
Oct 30, 2015, 7:27:36 AM10/30/15
to Django users
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

Sean Xu

unread,
Oct 30, 2015, 7:45:14 AM10/30/15
to Django users
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 :(

Andreas Kuhne

unread,
Oct 30, 2015, 9:47:53 AM10/30/15
to django...@googlegroups.com
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

Sean Xu

unread,
Oct 31, 2015, 11:27:50 PM10/31/15
to Django users
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

Andreas Kuhne

unread,
Nov 1, 2015, 5:09:02 AM11/1/15
to django...@googlegroups.com
Hi Sean,

That's interesting. You shouldn't have to add the locale paths explicitly. Good that it's wokring, but as long as you put the locale files in a directory named locale under each app and also under the project, it should just work (as long as all apps are included in the config). Just check so that you follow the correct way of setting up a project.

Regards,

Andréas

Sean Xu

unread,
Nov 2, 2015, 12:38:50 AM11/2/15
to Django users
Sorry, 

~/django-swingtime/django-swingtime-master/demo should be the project path where project level translation files were generated under ~/django-swingtime/django-swingtime-master/demo/locale.
The project level locale files should also be recognized, right?
The application path should be ~/django-swingtime/django-swingtime-master/demo/karate.

Br
Sean

Andreas Kuhne

unread,
Nov 2, 2015, 4:14:33 AM11/2/15
to django...@googlegroups.com
Hi,

I am unsure about the project level translations path, if they should be found as well. I have added them manually to settings, but only that path. Application paths get imported automatically.

Regards,

Andréas

Reply all
Reply to author
Forward
0 new messages