"trans" template tag using wrong language

53 views
Skip to first unread message

Joan Perals

unread,
Mar 8, 2017, 5:58:23 PM3/8/17
to Django users
I have a site using Django 1.10 with German and English as available languages, where German is the source language. Dynamic content is translated with django-modeltranslation and for static strings I am using Django's built-in `trans` template tag.

I want Django to return the page in the visitor's preferred language. I have read how Django discovers language preference several times, and I think I have everything right. As I change my language settings in my browser, the value of the `LANGUAGE_CODE` variable when displayed in a template always changes accordingly, and also the dynamic content is always returned in the expected language by django-modeltranslation. The locale middleware is of course in place, and so are the *.po and *.mo files as well. The browser request headers also look fine. But it's only the `trans` template tag that is not picking my browser's language preference. Instead, it seems to directly resort to the initial value of the `LANGUAGE_CODE` variable as it is originally set in the settings file, without going through the algorithm described in the link above.

Is this known? Certainly not what I expected, and it took me a lot of effort to narrow down the issue.

Thanks in advance for your help,

Joan

Joan Perals

unread,
Mar 9, 2017, 4:25:24 AM3/9/17
to Django users
This post on StackOverflow seems to report the same behaviour:

It's quite old (almost 5 years), but remains unresolved.

Antonis Christofides

unread,
Mar 9, 2017, 4:51:11 AM3/9/17
to django...@googlegroups.com

It's very unlikely that such a basic behaviour won't work, so probably you are doing something wrong (and so's the stack overflow poster). I made several related changes in an app yesterday and I had absolutely no problem with {% trans %} (but I'm using 1.8).

Do you have USE_I18N = True?

If in the template you use

  {% get_current_language as current_language %}
  <p>{{ current_language }}</p>

does it show the correct language?

Antonis Christofides
http://djangodeployment.com

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/02a7eb4a-086f-4e76-a693-3ae5623db08e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Joan Perals

unread,
Mar 11, 2017, 4:43:08 PM3/11/17
to Django users
Hi Antonis,

I think I figured this out. I have English as fallback language (LANGUAGE_CODE = 'en') in the settings, but my source language is German, and this mismatch seems to have been the source of all the trouble. Providing German translations (even if the original strings are already in German) seems to solve the issue.

Thanks for your help in any case,

Joan
Reply all
Reply to author
Forward
Message has been deleted
0 new messages