Internationalizing static JS in Django

I want to translate a piece of JS into Django.

I tried the command python manage.py makemessages -d djangojs

, but insettings.py

it only accepts the file in TEMPLATE_DIRS

I am trying to install JS in templates directory and it works fine.

I have djangojs.po and I can generate a .mo when compiled.

So the question is: how to make a post in a static file?

I found the same problem Here and also Here , but no one answers who will keep the good architecture.

Please save me!

My architecture:

  • MyApp
    • locale
    • Static
      • MyApp
        • Js
          • try.js
    • template
      • MyApp
        • try.html
    • views.py
    • urls.py
    • [...]

PS: Sorry for my english, I am not a native;)

+3


source to share


1 answer


My mistake is when I set STATIC_ROOT in settings.py. In fact this variable says in Django where Static, when it does CollectStatic on the server, I used it to tell where I found my static one (Django can find all whitout static data, it finds a static file in a folder, static in the project folder or in the application folder)

Finally:

set this in urls.py pro

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

urlpatterns = patterns('',

    [...]

    #Internationalization Javascript
    url(r'^jsi18n/$', 'django.views.i18n.javascript_catalog', js_info_dict),

)

      

In the template

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

      

In JS in static / my_app / my_file.js

document.write(gettext('Ma chaine de caractère a traduire'))

      



After that we run this command line

python manage.py makemessages -d djangojs

      

Here djangojs is a set of domains in urls.py at the beginning (this is a pseudo concept)

We currently have djangojs.po in the locale folder, which we can compile as a standard .po .

Document link: Here

Lik my ticket, where you can find a sample project and an explanation in English: Ticket

Good luck!

+1


source







All Articles