django - Why is request.session['django_language'] is being overriden by browser preferred language? -
using django, i'd users able choose site language.
it working fine when user preferred language set in browser english. user can switch between english , portuguese expected.
however, when change browser preferred language language switcher stops working (i tested in opera, google chrome, firefox , epiphany - couldn't test on ie or safari).
i suppose did wrong, cannot figure out what. puzzles me works fine when preferred language set default...
django documentation says use following choose language:
first, looks django_language key in current user’s session. failing that, looks cookie. failing that, looks @ accept-language http header. header sent browser , tells server language(s) prefer, in order priority. django tries each language in header until finds 1 available translations. failing that, uses global language_code setting.
why getting different behavior in language switcher when browser has preferred language when has default?
my settings.py respecting middleware 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 structure:
locale/ pt_br/ lc_messages/ django.mo django.po
my language switcher (in template) 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>
i found out problem was.
my language switcher setting english using "en-us".
this right if had more 1 english options. in case should have en_us folder (and other django.po , django.mo in it) inside locale folder.
as want use 1 english option can use "en" value of switcher. no additional locale folder needed, because "en" django's default.
for may find helpful, here fix:
the line:
< input type="hidden" name="language" value="en-us"/>
should be:
< input type="hidden" name="language" value="en"/>
Comments
Post a Comment