1. Make sure you have a correct-translated .mo
file:
Execute msgunfmt ./locale/jp/LC_MESSAGES/django.mo
to decompile .mo
file:
Example output:
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
msgid "world"
msgstr "日语世界"
2. Make sure Python's builtin gettext
worked
Execute python
under project root, enter the Python shell
, then execute:
import gettext
assert gettext.translation("django", "locale", ['jp']).gettext("world") == "日语世界"
3. Make sure LOCALE_PATHS
is correctly set
Execute python manage.py
under project root, enter the Django shell
, then execute:
from django.conf import settings
assert len(settings.LOCALE_PATHS) > 0
4. Make sure Django's gettext
worked under corresponding language context
Enter Django shell
, Execute:
from django.utils import translation
with translation.override("jp"):
assert translation.gettext("world") == "日语世界"
5. Make sure you have a standard language code, and django supports it
Enter Django shell
, Execute:
from django.utils import translation
assert 'ja' in translation.trans_real.get_languages()
assert 'zh-hans' in translation.trans_real.get_languages()
assert 'zh-hant' in translation.trans_real.get_languages()
assert 'zh-cn' not in translation.trans_real.get_languages()
6. Make sure you have the correct language context
Server code:
from django.contrib import admin
from django.http import JsonResponse
from django.urls import path
from django.utils.translation import gettext_lazy, get_language
def home(_):
return JsonResponse(dict(
language=get_language(),
hello=gettext_lazy("world")
), json_dumps_params=dict(ensure_ascii=False, indent=4))
urlpatterns = [
path('admin/', admin.site.urls),
path('', home)
]
Test it:
curl 'http://localhost:8000' -H 'Accept-Language: ja'
Example response:
{
"language": "ja",
"hello": "日语世界"
}
The required config but not in default Django project:
LOCALE_PATHS
The locale
path is required for django to find .mo
files
MIDDLEWARE
The LocaleMiddleware
is required for web project to detect context's language
BASE_DIR = Path(__file__).resolve().parent.parent
LOCALE_PATHS = (
BASE_DIR / "locale", # Here
)
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware', # And here
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
Another note:
Do not execute mkdir locale
to create locale
folder manually, python manage.py makemessages
will create locale
folder if your settings worked.
The correct i18n
steps for a new Django
project:
- Set
LOCALE_PATHS
- Set
LocaleMiddleware
python manage.py makemessages
python manage.py compilemessages
python manage.py runserver
to see if it worked.