-
Notifications
You must be signed in to change notification settings - Fork 1
i18n
In our project, we decided to enable internationalization support in our backend. Right now, we support Dutch and English.
The default language is Dutch. If you want English, include Accept-Language: en
in your request header.
Default Django error messages are automatically translated. If you write your own error messages, use _(<error-message>)
syntax. Where _()
is imported with from django.utils.translation import gettext_lazy as _
.
Note of caution:
At the time of writing this, there seem to be problems with f-strings. Sadly, you'll have to use the old and bulky format
instead.
Example:
_("{param} does not exist").format(param=param)
instead of _(f"{param} does not exist")
.
In our, project, we program in English. So in our code, we write all error messages in English. We provide a translation to Dutch for all of these error messages. That way we have a solid bilingual support by only caring about one-way translations.
When you wrote custom error messages that you wish to translate, run django-admin makemessages --all --ignore=venv/*
to add them to the *.po
files in backend/locale/
.
Now, take a look at the *.po
message files:
-
msgid
represents the translation string as it appears in the source code. -
msgstr
represents the language translation which you'll have to type yourself. It is empty by default, meaning there won't be a translation unless you explicitly give one.
After adding the translations, compile them by running django-admin compilemessages
, which will update the *.mo
files.
-
_()
from django.utils.translation import gettext_lazy as _
django-admin makemessages --all --ignore=venv/*
django-admin compilemessages