Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: how to generate/use translations messages from django.po for JavaScript (djangojs)?

I've been playing with localization on the client side. So I want to collect messages for djangojs from python and html files, but it didn't work

Django version is 1.3, Gettext utilitest are installed in the system

django-admin.py makemessages -d djangojs -l ru -e html,py

Does anyone know how to generate/use translations messages from django.po for JavaScript (djangojs)?

For example I put just ngettext("Hello world") messages for test reasons in some js file and finally it doesn't get into djangojs.po file.

Sultan

like image 528
sultan Avatar asked Mar 11 '12 12:03

sultan


People also ask

How does Django translation work?

Django then provides utilities to extract the translation strings into a message file. This file is a convenient way for translators to provide the equivalent of the translation strings in the target language. Once the translators have filled in the message file, it must be compiled.

What is Gettext_lazy in Django?

gettext_lazy() In definitions like forms or models you should use gettext_lazy because the code of this definitions is only executed once (mostly on django's startup); gettext_lazy translates the strings in a lazy fashion, which means, eg.

What is i18n Django?

{% load i18n %} is needed for internationalization. The purpose of internationalization is to allow a single application to read in multiple languages. In order to do this: you need a few hooks called translation strings. To give your template access to these tags, put {% load i18n %} toward the top of your template..


1 Answers

I'm the author of this question: Django 1.2.3 - Internationalization - makemessages does not detect all strings.

Your problem has probably the same cause as mine. It is because the makemessages for javascript is a hack and sometimes doesn't get the strings marked for translation. As the gettext tools do not support javascript, the code first goes through simple transforms and is then processed as Perl.

You can find an article about it here: A Javascript lexer in Python, and the saga behind it

And the bug report here: Ticket 7704. It is fixed in Django 1.4.

As my project was small, I edited the PO files directly but if yours is bigger you should use either django 1.4 RC or the patch linked in the bug report.

like image 187
Marc Demierre Avatar answered Nov 15 '22 10:11

Marc Demierre