How to realize i18n multilingual internationalization in Django 2.2
This article shows you how to achieve i18n multilingual internationalization in Django 2.2. the content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
Set up in setting.py:
# LANGUAGE_CODE = 'en-us'LANGUAGE_CODE =' the directory where the zh-hans'# translation file is located. You need to manually create LOCALE_PATHS = (os.path.join (BASE_DIR, 'locale'),) TIME_ZONE =' Asia/Shanghai'USE_I18N = TrueUSE_L10N = True
Add 'django.middleware.locale.LocaleMiddleware':' to MIDDLEWARE
MIDDLEWARE = ['django.middleware.security.SecurityMiddleware',' 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',' django.middleware.clickjacking.XFrameOptionsMiddleware',]
Add 'django.template.context_processors.i18n'' to TEMPLATES
TEMPLATES = [{'BACKEND':' django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join (BASE_DIR,' templates')], 'APP_DIRS': True,' OPTIONS': {'context_processors': [' django.template.context_processors.i18n', 'django.template.context_processors.debug' 'django.template.context_processors.request',' django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages',],},},]
Mkdir-p locale
Django-admin makemessages-l zh_Hans
Django-admin compilemessages
Or
Python3 django-admin makemessages-l zh_Hans
Python3 django-admin compilemessages
The above is how to achieve i18n multilingual internationalization in Django 2.2. have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.