Is Django session translation customization sticky for all languages ​​except Chinese?

I have a really strange problem that I have never seen before. I am using Django 1.10.

I have several dictionary files:

/locale/fr/LC_MESSAGES/django.po
/locale/de/LC_MESSAGES/django.po
/locale/zh/LC_MESSAGES/django.po

Application strings are written in English. Dictionary files are completed and compiled into files mo.

I keep each user's preference in the field language UserProfile. When updating my profile, I apply language translation to the session.

# 'up' is a UserProfile object pertaining to the user
up.update(language=form.cleaned_data['language'])
translation.activate(up.language)
self.request.session[translation.LANGUAGE_SESSION_KEY] = up.language
return super(self, UpdateUserProfile).form_valid(form)

This is great for French and German. return superdisplays the form template in French / German, and then I can go to other pages and see the French / German text.

( "zh" ). return super ( ), , .

dev , - . VM. . ?

- :

settings.py

LANGUAGE_CODE = 'en-us'
LOCALE_PATHS = [ 
    BASE_DIR + '/locale/',
]
USE_I18N = True
USE_L10N = True

, POST UpdateLanguage FormView request.session[translation.LANGUAGE_SESSION_KEY] zh, . GET ( request.session[translation.LANGUAGE_SESSION_KEY] , ​​ zh, ).

URL-.

+4
2

, zh , LANGUAGES , , .

, , , translation.activate("zh"). LANGUAGES. zh. "zh" , . translation.activate("turnip"), Django , "".

, , , LANGUAGES , : , .

, :

import os

import django
from django.utils.translation import trans_real as translation, LANGUAGE_SESSION_KEY
from django.middleware.locale import LocaleMiddleware

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "btw.settings")
django.setup()

#
# Calling translation.activate directly.
#
translation.activate("zh")
value = translation._active.value
print "translation:", value.language()

#
# Middleware processing
#
class FakeRequest(object):

    path_info = ""
    session = {}
    COOKIES = {}
    META = {}

middleware = LocaleMiddleware()

for lang in ("fr", "zh", "zh-hans"):
    print "Trying:", lang
    request = FakeRequest()
    request.session[LANGUAGE_SESSION_KEY] = lang

    middleware.process_request(request)
    print request.LANGUAGE_CODE

, , "zh" , request.LANGUAGE_CODE , "zh" - . :

translation: zh
Trying: fr
fr
Trying: zh
en-us
Trying: zh-hans
zh-hans
+3

, django , , global_settings.py

zh . zh-hans zh-hant, , , HttpResponse , , , , .

+2

Source: https://habr.com/ru/post/1665542/


All Articles