Django makemessages not working for JS files

穿精又带淫゛_ 提交于 2019-12-07 05:52:19

问题


My setup

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 the locale folder in the root folder of my project, where manage.py is located.

What I'm doing

Simply running:

./manage.py -l ro -d djangojs

My problem

It's not working. No .po file is being generated. Server-side translation works, however (views + templates). I've followed all advice, and still nothing. Even tried to create the djangojs.po file myself to see if Django deletes it, or does something with it -- nope.

No error is generated, just processing locale ro is shown (for a really short time -- too short if you ask me), and that's that. Any help?

Edit: Forgot to mention that my folder containing the JS files is not inside each Django app, but in a separate location. Still, shouldn't Django look inside the STATICFILES_DIRS?


回答1:


Django's makemessages only will make messages from files that are in on one of your TEMPLATE_DIRS. So any files you want to translate need to be in one of those directories.

You can do that in one several ways:

  • Place the *.js files in one of your TEMPLATE_DIRS as is
  • In-lining your JS in the html files
  • Place all the strings that need translation in data-attributes on the dom and grab them from the dom via JS



回答2:


  1. Are you running makemessages from a directory parent of the ones containing your JavaScript files?
  2. Do your JavaScript file names ends with a .js?
  3. Do you either use django.gettext('string') or _('string') to mark strings requiring translations?



回答3:


I've experienced the same issue. I've discovered that the issue is reported in the Django ticket #23717: https://code.djangoproject.com/ticket/23717

Fixes are in upcoming stable 1.7.2 version: https://docs.djangoproject.com/en/1.7/releases/1.7.2/

I've installed 1.7.2 and confirmed that the issue is fixed.




回答4:


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



来源:https://stackoverflow.com/questions/26095934/django-makemessages-not-working-for-js-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!