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

后端 未结 5 1231
野性不改
野性不改 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 16:44

    As @SteveMayne pointed out in comment (but it worth an answer), you can now use the context manager translation.override (works with Django 1.6, didn't check with earlier versions):

    from django.utils import translation
    print(_("Hello"))  # Will print to Hello if default = 'en'
    
    # Make a block where the language will be Danish
    with translation.override('dk'):
        print(_("Hello"))  # print "Hej"
    

    It basically uses the same thing than @bitrut answer but it's built-in in Django, so it makes less dependencies...

    0 讨论(0)
  • 2020-12-08 16:45

    simplest way to switch language is:

    from django.utils.translation import activate
    activate('en')
    # do smthg
    activate('pl')
    # do something in other language
    

    be carefull with this as it is changing context for the rest of the execution of this process/thread.

    0 讨论(0)
  • 2020-12-08 16:53

    You can force language in a nice way using context manager:

    class force_lang:
        def __init__(self, new_lang):
            self.new_lang = new_lang
            self.old_lang = translation.get_language()
        def __enter__(self):
           translation.activate(self.new_lang)
        def __exit__(self, type, value, tb):
           translation.activate(self.old_lang)
    

    Then you can use with statement:

    with force_lang('en'):
        ...
    
    0 讨论(0)
  • 2020-12-08 16:56

    Turns out the django docs explain how:

    While Django provides a rich set of i18n tools for use in views and templates, it does not restrict the usage to Django-specific code. The Django translation mechanisms can be used to translate arbitrary texts to any language that is supported by Django (as long as an appropriate translation catalog exists, of course). You can load a translation catalog, activate it and translate text to language of your choice, but remember to switch back to original language, as activating a translation catalog is done on per-thread basis and such change will affect code running in the same thread.

    0 讨论(0)
  • 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' %}
        <p>
            <a href="/login/">{% trans "Log in" %}</a>
        </p>
    {% endoverridelocale %}
    

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

    {% overridelocale 'ru' %}
        <p>
            <a href="/login/">{% trans "Log in" %}</a>
        </p>
    {% endoverridelocale %}
    

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

    {% overridelocale 'en' %}
        <p>
            <a href="/login/">{% trans "Log in" %}</a>
        </p>
    {% endoverridelocale %}
    
    0 讨论(0)
提交回复
热议问题