11

I'm trying to set up Internationalization of JavaScript code in my Django application.

My Django app has a locale subdirectory with a properly generated djangojs.po file. The package definition is as follows:

# urls.py
js_info_dict = {
    'packages': ('my_project',),
} 

./manage.py makemessages worked well as the .po file contains all the to-be-translated strings but no JavaScript string ever gets translated on the website and the catalog is always empty.

jnns
  • 5,148
  • 4
  • 47
  • 74
kingsley
  • 305
  • 3
  • 12

2 Answers2

6

I also had some problems with. This is how it works for me:

Add this to yr root urls.py:

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


urlpatterns = patterns('',                   

    #enable using translation strings in javascript
    #source: https://docs.djangoproject.com/en/dev/topics/i18n/translation/#module-django.views.i18n
    (r'^jsi18n/$', 'django.views.i18n.javascript_catalog', js_info_dict),

)

In JS files use:

var somevar = gettext('Text to translate');

To compile django translation files: In a shell/terminal run from the project root (where 'apps', 'settings', etc lie):

#for "normal django files" (.py, .html):
django-admin.py makemessages --locale=de
#for javascript files. source: http://stackoverflow.com/a/3571954/268125
django-admin.py makemessages -a -d djangojs --locale=de
#to compile the translation files to machine code
django-admin.py compilemessages --locale=de
j7nn7k
  • 17,995
  • 19
  • 78
  • 88
  • 1
    Still had to add my project to INSTALLED_APPS. (which I don't like) – bjunix Jul 24 '13 at 10:12
  • It still does not work for me. And it seems that 'djangojs' is the default domain. No need to write it explicitly. – azmeuk Jun 20 '16 at 15:50
3

i added my_project to INSTALLED APPS in settings.py and that seemed to do the trick

kingsley
  • 305
  • 3
  • 12
  • I already have "my_project" in my_project's INSTALLED APPS in settings.py and still the catalog is empty. Might you have an idea as to what else needs to be done? – urig May 04 '11 at 06:27
  • I've found out what went wrong for me. I had put my po files under /conf/locale when it should have been /locale . – urig May 04 '11 at 07:44