About Temporary Language Switching

You can override a language temporarily using the override() function:

1
2
3
4
5
6
7
8
9
from django.utils.translation import override, gettext
from django.conf import settings

def greeting_translated():
    greeting = {}
    for lang_code, lang_name in settings.LANGUAGES:
        with override(language=lang_code):
            greeting[lang_code] = gettext("Hello, World!")
    return greeting

Tips and Tricks Translations Django 4.2 Django 3.2 Django 2.2