ji18n translation in template file

477 views
Skip to first unread message

kreid

unread,
Dec 14, 2011, 12:00:37 PM12/14/11
to Django users
My template file contains javascript/jquery. When translating with
gettext from a .js file there are no problems as the 'makemessages'
switch picks it up and places it into the djangojs.po file. However,
when the javascript is embedded in the template file (i.e. a txt file)
makemessages picks up on 'trans' tags to place in the .po file. The
trans tags are not being translated in the javascript...only in the
html.

What am I doing wrong?

Tom Evans

unread,
Dec 14, 2011, 12:17:30 PM12/14/11
to django...@googlegroups.com

I'm sure you've read the docs on JS i18n:

https://docs.djangoproject.com/en/1.3/topics/i18n/internationalization/#specifying-translation-strings-in-javascript-code

In particular, for JS translations, you need to:

* Ensure that the javascript translation catalog is loaded:

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

* Get your translations out with the gettext() function:

document.write(gettext('Translate this'));

You don't use the django template tags for translations in javascript
files ({% trans %}/{% blocktrans %}) as these are not rendered
templates but javascript, and will typically not be served at all by
django, but directly by your webserver.

If you are trying to get translations into javascript embedded inside
a django template, you can do so using the {% trans %} tags, but
django does not know the context you are outputting in. Eg this may
cause problems:

<script type='text/javascript'>
var foo = '{% trans "It is me" %}';
</script>

The english translation would work, but the French would end up
rendered like this (note the broken quotation):

<script type='text/javascript'>
var foo = 'C'est moi';
</script>

For this reason, it is better to use the js specific way of accessing
translations.

Cheers

Tom

kreid

unread,
Dec 14, 2011, 1:38:44 PM12/14/11
to Django users
Yea, I've read through it all thoroughly and everything seems to work
ok except for the jquery in each template. So, I've moved it all
to .js files which seems to be working now.

Thanks for the reply,

Kreid

On Dec 14, 9:17 am, Tom Evans <tevans...@googlemail.com> wrote:


> On Wed, Dec 14, 2011 at 5:00 PM, kreid <kharronr...@gmail.com> wrote:
> > My template file contains javascript/jquery.  When translating with

> >gettextfrom a .js file there are no problems as the 'makemessages'


> > switch picks it up and places it into the djangojs.po file.  However,
> > when the javascript is embedded in the template file (i.e. a txt file)
> > makemessages picks up on 'trans' tags to place in the .po file.  The
> > trans tags are not being translated in the javascript...only in the
> > html.
>
> > What am I doing wrong?
>
> I'm sure you've read the docs on JS i18n:
>

> https://docs.djangoproject.com/en/1.3/topics/i18n/internationalizatio...


>
> In particular, for JS translations, you need to:
>
> * Ensure that the javascript translation catalog is loaded:
>
>   <script type="text/javascript" src="{% url
> django.views.i18n.javascript_catalog %}"></script>
>

> * Get your translations out with thegettext() function:

Reply all
Reply to author
Forward
0 new messages