Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django's support for translations in Javascript files

I read and followed the instructions in here, but can't seem to see the string in the javascript in the po file.

structure of my project is: 
cb/    
   cb_app    
   cb    
   static_files    
   templates

First I copied these into my url.py:

js_info_dict = {
    'packages': ('cb_app',),
}

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

Then I added this script to my html:

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

The actual script where I would like to get the translation, is as simple as that:

$(document).ready(function () { 
    $('#id_sales_item').chosen({no_results_text: gettext('No results match')}); 
});

...and is utilized in the same html.

So is there anything more I need to do?

All I did then was to run the line below both from cb/cb and from cb/cb_app.

django-admin.py makemessages -l en_GB

But still no sign of 'No results match' in either cb/cb/locale nor in cb/cb_app/locale

Any tips?

like image 820
Houman Avatar asked Oct 07 '22 16:10

Houman


1 Answers

I have finally found the problem.

The documentation suggests creating the messages once from the Django project and once from Django app. That way you end up with two locale directory. And in neither of those would the javascript translations be picked up anyway. This is quite a mess.

The cleanest solution I have found is to go to settings.py and insert this line (see also my project hierarchy above):

LOCALE_PATHS = ( '/home/kave/projects/cb/locale',)

Then create a directory called locale in the project-root-directory (see the path above)

Don't forget applying the entries into url.py and html as well (see above).

Finally now that the local's are unified into one place, go to project-root-directory: /home/kave/projects/cb and run these two commands:

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

The first command get the translation texts from both project and app subfolders. The second gets the javascript translation into a second po file.

Thats it.

like image 61
Houman Avatar answered Oct 10 '22 03:10

Houman