1👍
Yes – Django has an inbuilt method for this .gettext()
as described in the docs:
https://docs.djangoproject.com/en/3.1/topics/i18n/translation/
This will translate the string into the end user’s language. You can use it within templates or within your models.
To use it within your template:
<!-- load i18n -->
{% load i18n %}
<!-- translate text -->
<title>{% translate "This is the title." %}</title>
<!-- translate string variable passed from context -->
<title>{% translate myvar %}</title>
In templates, translate
calls .gettext()
on the string under the hood.
Hope this helps 😉
Extra info:
Django’s LocaleMiddleware
will attempt to detect which language it is translating to (via a variety of techniques which you can read on the doc link).
However, if the middleware cannot detect which language the end user wants, it will use the language specified in the LANGUAGE_CODE
variable in settings.py
. This is the default language that Django will translate to.