问题
I use Django to create a web project in two languages: english and german. The default language of the document is english and I translated it in german by my own and created a .po file.
My idea is that if someone speaks german and the browser's locale is set to "de", Django should switch to german (the translation i provide). BUT in every other case, it should switch to english.
The problem is that it exactly works the other way round and I have no clue why!
Status quo: If the browser's locale is set to english, the website is displayed in english. but in any other case it is displayed in german.
LANGUAGE_CODE = 'en-us'
_ = lambda s: s
LANGUAGES = (
('en', _('English')),
('de', _('German')),
)
The middlewares are correctly set, the locale-path as well! When I test the website, I clear all the cookies and the cache. I tried to provide two translations (germand AND english, even though it is already written in english), but still the same effect.
Thanks for your help.
回答1:
If you want your users to be able to specify language, make sure that LocaleMiddleware is enabled:
MIDDLEWARE_CLASSES = (
...
'django.middleware.locale.LocaleMiddleware',
...
)
Then Django will look for the user's language preference in that order:
So the most straightforward way to set language explicitly in Django session, is to rewrite request.session['django_language']:
def someview (request):
...
request.session['django_language'] = 'en'
...
来源:https://stackoverflow.com/questions/10312669/django-wrong-language-preference