Django makemessages not working for JS files

My setting

settings.py

INSTALLED_APPS = ( ... 'myprojectname', ... ) STATIC_ROOT = '/var/www/a_valid_path/' LOCALE_PATHS = ( os.path.join(BASE_DIR, "locale"), ) 

urls.py

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

My project structure is as follows:

 |- projectname |--- app1 |--- app2 |--- manage.py |- virtualenv |- static |--- js |--- css 

I also have a locale folder in the root folder of my project, where manage.py is located.

What am I doing

It just works:

 ./manage.py -l ro -d djangojs 

My problem

This does not work. The .po file is not created. However server-side translation works (views + templates). I followed all the advice, and still nothing. I even tried to create the djangojs.po file djangojs.po to see if Django would delete it or something with it - no.

The error is not generated, only processing locale ro displayed (in a very short time - too short if you ask me), and that is. Any help?

Edit:. I forgot to mention that my folder containing the JS files is not located inside each Django application, but in a separate place. However, should Django not look inside STATICFILES_DIRS ?

+5
source share
4 answers

Django makemessages will only make messages from files located on one of your TEMPLATE_DIRS . Therefore, any files you want to translate must be in one of these directories.

You can do this in several ways:

  • Put * .js files in one of TEMPLATE_DIRS as is
  • Embed JS in html files
  • Put all lines requiring translation in data attributes on dom and grab them from dom via JS
+1
source
  • Do you use makemessages from the parent directory of those that contain your JavaScript files?
  • Do your JavaScript file names end in .js ?
  • Do you django.gettext('string') or _('string') to indicate strings that require translations?
0
source

I had the same problem. I found that the problem is being reported on Django ticket # 23717: https://code.djangoproject.com/ticket/23717

The fixes are in the expected stable version 1.7.2: https://docs.djangoproject.com/en/1.7/releases/1.7.2/

I installed 1.7.2 and confirmed that the problem was fixed.

0
source

I had the same problem when using Django i18n, after many attempts, I finally got the correct answer: we need to put the .js files in the project directory, which was specified when we assign 'js_info_dict'.
But usually we put the javascript files in the same level directory as the project, so there is a problem. (we do not need to put JavaScript files in the templates directory).

0
source

Source: https://habr.com/ru/post/1203598/


All Articles