python 2.7 - Django not translating the site properly -
after spending many hours on this, stackoverflow rescue.
i configured settings.py below:
... time_zone = 'europe/berlin' language_code = 'de' languages = ( ('en', u'english'), ('de', u'german'), ('fr', u'french'), ) use_i18n = true use_l10n = true middleware_classes = ( 'django.middleware.locale.localemiddleware', 'django.middleware.common.commonmiddleware', 'django.contrib.sessions.middleware.sessionmiddleware', 'django.middleware.csrf.csrfviewmiddleware', 'django.contrib.auth.middleware.authenticationmiddleware', 'django.contrib.messages.middleware.messagemiddleware', ) template_context_processors = ( 'django.contrib.auth.context_processors.auth', 'django.core.context_processors.debug', 'django.core.context_processors.i18n', 'django.core.context_processors.request', 'django.core.context_processors.static', 'django.contrib.messages.context_processors.messages', ) ...
in base.html
file, have form below:
<form action="/i18n/setlang/" method="post"> {% csrf_token %} <input name="next" type="hidden" value="/" /> <select name="language"> {% get_language_info_list languages languages %} {% language in languages %} <option value="{{ language.code }}">{{ language.name_local }} ({{ language.code }})</option> {% endfor %} </select> <input type="submit" value="go" /> </form>
my urls.py
:
urlpatterns = patterns('', url(r'^i18n/', include('django.conf.urls.i18n')), url(r'^$', 'mainapp.views.index'), #root )
in same base.html
file, have on top {% load i18n %}
, in body, have sample {% trans "this title." %}
. before running server, did:
django-admin.py makemessages -l de django-admin.py makemessages -l fr
the sample text above picked makemessages
, , provided respective translations msgstr
. after that, did django-admin.py compilemessages.
command ran nicely , generated .mo files in respective locale folders.
i run server , the form not work. stackoverflow post, hinted remove #, fuzzy lines
, did. doing wrong?
thanks!
you should put localemiddleware
after sessionmiddleware
in middleware_classes:
middleware_classes = ( 'django.middleware.common.commonmiddleware', 'django.contrib.sessions.middleware.sessionmiddleware', 'django.middleware.locale.localemiddleware', .... )
the order of middleware classes important. localemiddleware
uses session data detect user language, must come after sessionmiddleware
. mentioned in docs here https://docs.djangoproject.com/en/1.3/topics/i18n/deployment/#how-django-discovers-language-preference
let's hope works you!
Comments
Post a Comment