Django JavaScript translation not working

I tried to follow the manual , but it is not clear enough.

  • I added this to my urls.py

    urlpatterns = patterns('', (r'^jsi18n/(?P<packages>\S+?)/$', 'django.views.i18n.javascript_catalog'), ) 
  • Generated lang files with this command:

     django-admin.py makemessages -d djangojs -l fr 

root_folder/locale/fr/LC_MESSAGES now contains django.po and djangojs.po and alert(gettext('this is to be translated')); in one of my js files that were compiled in djangojs.po .

  • I ran django-admin.py compilemessages and restarted the server.

  • Added this to my base.html:

     <script type="text/javascript" src="{% url 'django.views.i18n.javascript_catalog' 'locale' %}" >< /script > 

Note that I added 'locale' to exclude the exception of passing the package name when dynamically loading translations.

  • Visited /jsi18n/locale/ from my browser, and all I get is the Django translation functions:

     /* gettext library */ var catalog = new Array(); function pluralidx(count) { return (count == 1) ? 0 : 1; } function gettext(msgid) { .... 

Why is "it should be translated" not displayed, and on what basis will it show a specific language without passing it from the URL?

+7
python django gettext translation
source share
2 answers

I don’t know exactly how to solve your problem, but I can tell you how everything works for me:

The locale folder is inside my tickets application.

urls.py

 js_info_dict = { 'domain': 'djangojs', 'packages': ('tickets',), } urlpatterns = patterns('', (r'^jsi18n/$', 'django.views.i18n.javascript_catalog', js_info_dict), ... 

base.html

 <script type="text/javascript" src="{% url django.views.i18n.javascript_catalog %}"></script> 

and create a message file:

 python -m django-admin makemessages -d djangojs -l fr python -m django-admin compilemessages 

I hope you can choose something.

+10
source share

For others with my specific case, js messages are generated and compiled in order, but not displayed in templates or pages when using the i18n language urls.

This is because the javascript directory should be added to i18n URL patterns, not regular patterns.

 urlpatterns += patterns('', (r'^jsi18n/$', 'django.views.i18n.javascript_catalog', js_info_dict), ) 

=>

 urlpatterns += i18n_patterns('', (r'^jsi18n/$', 'django.views.i18n.javascript_catalog', js_info_dict), ) 
+4
source share

All Articles