Ticket #10078: 10078.diff

File 10078.diff, 2.4 KB (added by Ramiro Morales, 14 years ago)

Patch for this documentation ticket, ready for trview by documentation experts

  • docs/howto/custom-management-commands.txt

    diff --git a/docs/howto/custom-management-commands.txt b/docs/howto/custom-management-commands.txt
    a b  
    9898:doc:`management commands</ref/django-admin>` can accept some
    9999default options such as :djadminopt:`--verbosity` and :djadminopt:`--traceback`.
    100100
     101.. admonition:: Management commands and locales
     102
     103    Django :class:`BaseCommand` :meth:`~BaseCommand.execute` method sets the
     104    hardcoded ``en-us`` locale, the reason for this is that the commands shipped
     105    with Django performs several tasks that really require a system-neutral
     106    string language (for which we use ``en-us``), user-visible content and
     107    database population being two examples.
     108
     109    If, for any reason, you need that you custom management command uses
     110    another locale, you should manually activate and deactivate it in your
     111    :meth:`~BaseCommand.handle` or :meth:`~NoArgsCommand.handle_noargs` method
     112    using the functions provided by the I18N support code:
     113
     114    .. code-block:: python
     115
     116        from django.core.management.base import BaseCommand, CommandError
     117        from django.utils import translation
     118
     119        class Command(BaseCommand):
     120            ...
     121            self.can_import_settings = True
     122
     123            def handle(self, *args, **options):
     124
     125                # Activte a fixed locale, e.g. Russian
     126                translation.activate('ru')
     127
     128                # Or you can activate the LANGUAGE_CODE
     129                # chosen in the settings:
     130                #
     131                #from django.conf import settings
     132                #translation.activate(settings.LANGUAGE_CODE)
     133
     134                # Your command logic here
     135                # ...
     136
     137                translation.deactivate()
     138
     139    Take in account though, that system management commands typically have to
     140    be very careful about running in non-uniform locales, so:
     141
     142    * Make sure the :setting:`USE_I18N` setting is always ``True`` when running
     143      the command (this is one good example of the potential problems stemming
     144      from a dymamic runtime enviroment that Django commands avoid offhand by
     145      always using a fixed locale).
     146
     147    * Review the code of you command and the code it calls for behavioral
     148      differences it might show when locales are changed and evaluate its impact
     149      on predictable behavior of your command.
     150
    101151Command objects
    102152===============
    103153
Back to Top