Django switching, for a block of code, switch the language so translations are done in one language

后端 未结 5 1241
野性不改
野性不改 2020-12-08 16:09

I have a django project that uses a worker process that sends emails to users. The worker processes listens to a rabbitmq server and gets all the details about the email to

5条回答
  •  温柔的废话
    2020-12-08 17:07

    It's quite simple using django-i18next (https://pypi.python.org/pypi/django-i18next).

    Load the templatetags.

    {% load i18n i18next %}
    

    The following code forces Dutch locale for whatever is put inside the overridelocale block.

    {% overridelocale 'nl' %}
        

    {% trans "Log in" %}

    {% endoverridelocale %}

    The following code forces Russian locale for whatever is put inside the overridelocale block.

    {% overridelocale 'ru' %}
        

    {% trans "Log in" %}

    {% endoverridelocale %}

    The following code forces English locale for whatever is put inside the overridelocale block.

    {% overridelocale 'en' %}
        

    {% trans "Log in" %}

    {% endoverridelocale %}

提交回复
热议问题