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

后端 未结 5 1245
野性不改
野性不改 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条回答
  •  萌比男神i
    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...

提交回复
热议问题