I'm trying to convert my web-app to a couple of different languages in Python Django. I have managed to do so for English and French but the problem comes in when I add the zh hans or zh-cn. I get the following error:
Here's my settings.py
from pathlib import Path
import cloudinary
import os
from django.utils.translation import gettext_lazy as _
BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'go',
'crispy_forms',
'cloudinary_storage',
'cloudinary',
]
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',
]
ROOT_URLCONF = 'core.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'core.wsgi.application'
# Database
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
# Password validation
# https://docs.djangoproject.com/en/4.2/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/4.2/topics/i18n/
# Set the default language for your project.
LANGUAGE_CODE = 'en'
LANGUAGES = [
('en', _('English')),
('fr', _('French')),
('zh-cn', u'简体中文'),
]
# Enable localization by setting USE_I18N to True.
USE_I18N = True
# Set the directory where Django will look for translation files.
LOCALE_PATHS = [
os.path.join(BASE_DIR, 'locale'), # You can create a 'locale' directory in your project's base directory.
]
TIME_ZONE = 'Africa/Nairobi'
My Locale structure:
And my views.py
from django.shortcuts import render,redirect
from .models import *
from .forms import *
from django.core.mail import send_mail
from django.utils.translation import gettext as _
from django.utils.translation import get_language, activate, gettext
def homex(request):
trans = translate(language='zh_cn')
return render(request, 'homex.html', {'trans': trans})
def translate(language):
cur_language = get_language()
try:
activate(language)
text = gettext('hello')
finally:
activate(cur_language)
return text
What could I be doing wrong?
The expected result is for the Hello text to be translated into Chinese