Boky
Boky

Reputation: 12084

Translating text in external js file in Django

The project tree is as follows :

enter image description here

In the url.py inside master folder I have next:

from django.views.i18n import javascript_catalog

......
js_info_dict = {
    'domain': 'djangojs',
    'packages': ('traxio.traxio',),
}

url(r'^jsi18n/$', javascript_catalog, js_info_dict, name='javascript-catalog'),

In my base.html file (master/templates/master/base.html) I added :

<script type="text/javascript" src="{% url 'javascript-catalog' %}"></script>
<script src="/static/master/js/custom.js"></script>

If I click on page source when page loads I see :

<script type="text/javascript" src="/master/jsi18n/"></script>
<script src="/static/master/js/custom.js"></script>

The custom.js is the file where I need those translations.

I did :

django-admin makemessages -d djangojs -l nl
django-admin makemessages -d djangojs -l fr
django-admin compilemessages

The files are created in the locale folder. My custom.js is as follows:

function contactUs(fullname, telephone, email) {    
    bootbox.dialog({
        title: gettext("Limit reached"),
        message: '<p>gettext(You have reached the limit.)</p>',
        buttons: {
            .......
            }
        }
    });
}

But nothing happends. What did I wrong?

Upvotes: 0

Views: 682

Answers (1)

Boky
Boky

Reputation: 12084

Instead of adding it to master/urls.py I added it to traxio/urls.py and I changed packages.

from django.views.i18n import javascript_catalog

js_info_dict = {
    //domain is default
    'packages': ('traxio',),
}

urlpatterns = [
    .....
    url(r'^jsi18n/$', javascript_catalog, js_info_dict, name='javascript-catalog'),
]

That did the trick.

If somebody else has the same problem hope this will help.

Upvotes: 1

Related Questions