Translating Python code
To translate literals in your Python code, you can mark strings for translation using the gettext()
function included in django.utils.translation
. This function translates the message and returns a string. The convention is to import this function as a shorter alias named _
(the underscore character).
You can find all the documentation about translations at https://docs.djangoproject.com/en/4.1/topics/i18n/translation/.
Standard translations
The following code shows how to mark a string for translation:
from django.utils.translation import gettext as _
output = _('Text to be translated.')
Lazy translations
Django includes lazy versions for all of its translation functions, which have the suffix _lazy()
. When using the lazy functions, strings are translated when the value is accessed, rather than when the function is called (this is why they are translated lazily). The lazy translation functions come in handy when the strings...