Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django JavaScript translation not working

I tried to follow the guide but it's not clear enough.

  1. I added this to my urls.py

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

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

root_folder/locale/fr/LC_MESSAGES now contain django.po & djangojs.po and alert(gettext('this is to be translated')); in one of my js files was picked up in djangojs.po.

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

  2. 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 avoid the exception of not passing the package name when dynamically loading translations.

  1. Visited /jsi18n/locale/ from my browser and all I get is Django translation functions:

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

Why 'this is to be translated' is not showing and on which basis it will show a specific language without passing it with the URL?

like image 559
Adam Silver Avatar asked Oct 27 '13 23:10

Adam Silver


2 Answers

I don't know exactly how to solve your problem, but I can tell you, how things work for me:

The locale folder is inside my tickets app.

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 to create message file:

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

Hopefully you can pick something up from this.

like image 177
mariodev Avatar answered Oct 15 '22 10:10

mariodev


For others with my particular case, js messages are generated and compiled OK but not rendered in templates or pages when you use i18n language urls.

This is because javascript catalog should be added to i18n urls patterns, not to normal 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), )
like image 22
miguelfg Avatar answered Oct 15 '22 12:10

miguelfg