简体   繁体   中英

How to activate translation for all management commands by default?

I need translation to be activated in all of my django management commands by default. Currently I'm putting this line of code in all my management commands:

translation.activate(settings.LANGUAGE_CODE)

And sometimes I forget to consider it in my command. I need it to send translated notifications. Is there any way to activate translation for all of management commands by default?
Any help is appreciated.

After looking into BaseCommand 's source I figured out django is disabling translation and I could just prevent that by adding an attribute inside my Command class:

class CoreBaseCommand(BaseCommand):

    leave_locale_alone = True

I did put that line in my custom BaseCommand to be applied in all inherited commands.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM