Reputation: 989
Using Django, I'd like users to be able to choose the site language.
It is working perfectly fine when the user preferred language set in browser is English. User can switch between English and Portuguese as expected.
However, when I change my browser preferred language the language switcher stops working (I tested in Opera, Google Chrome, Firefox and Epiphany - couldn't test on IE or Safari).
I suppose I did something wrong, but cannot figure out what. It puzzles me that it works fine when the preferred language is set back to default...
Django documentation says it will use the following to choose the language:
First, it looks for a django_language key in the current user’s session. Failing that, it looks for a cookie. Failing that, it looks at the Accept-Language HTTP header. This header is sent by your browser and tells the server which language(s) you prefer, in order by priority. Django tries each language in the header until it finds one with available translations. Failing that, it uses the global LANGUAGE_CODE setting.
Why am I getting a different behavior in the language switcher when browser has a preferred language than when it has the default?
My settings.py respecting middleware is as follows
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
)
My locale folder contains the structure:
locale/
pt_BR/
LC_MESSAGES/
django.mo
django.po
My language switcher (in the template) is as follows:
<div id="secondary-menu" class="navigation">
<ul class="menu">
<li><a href="/about-us" > {{ second1 }}</a></li>
<li><a href="/free-culture" > {{ second2 }}</a></li>
<li><a > {{ second3 }}</a>
<ul>
<li><form name="setLangEnglish" method="post" action="/i18n/setlang/">{% csrf_token %}
<input name="next" type="hidden" value="/" />
<input type="hidden" name="language" value="en-us"/>
<input class="language-switcher" type="submit" alt="English" name="submit" value="English"/>
</form>
</li>
<li><form name="setLangPortuguese" method="post" action="/i18n/setlang/">{% csrf_token %}
<input name="next" type="hidden" value="/" />
<input type="hidden" name="language" value="pt-br"/>
<input class="language-switcher" type="submit" alt="Portugues" name="submit" value="Portugues"/>
</form>
</li>
</ul>
</li>
</ul>
</div>
Upvotes: 0
Views: 814
Reputation: 989
I found out what the problem was.
My language switcher was setting English using "en-us".
This would only be right if I had more than one English options. In which case I should have a en_US folder (and other django.po and django.mo in it) inside my locale folder.
As I want to use only one English option I can use "en" as the value of the switcher. No additional locale folder is needed, because "en" is django's default.
For anyone who may find this helpful, here is the fix:
The line:
< input type="hidden" name="language" value="en-us"/>
Should be:
< input type="hidden" name="language" value="en"/>
Upvotes: 0