Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.2.X] Fixed #10078 - Document use of locales in management commands…

…. Thanks gregoire for the suggestion and ramiro for the patch.

Backport of r15141 from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@15142 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 0b0e9457b2fedea68b10f6ef019d1d1a67f4863c 1 parent c76ab45
Tim Graham authored January 03, 2011

Showing 1 changed file with 49 additions and 0 deletions. Show diff stats Hide diff stats

  1. 49  docs/howto/custom-management-commands.txt
49  docs/howto/custom-management-commands.txt
@@ -91,6 +91,55 @@ In addition to being able to add custom command line options, all
91 91
 :doc:`management commands</ref/django-admin>` can accept some
92 92
 default options such as :djadminopt:`--verbosity` and :djadminopt:`--traceback`.
93 93
 
  94
+.. admonition:: Management commands and locales
  95
+
  96
+    The :meth:`BaseCommand.execute` method sets the hardcoded ``en-us`` locale
  97
+    because the commands shipped with Django perform several tasks
  98
+    (for example, user-visible content and database population) that require
  99
+    a system-neutral string language (for which we use ``en-us``).
  100
+
  101
+    If your custom management command uses another locale, you should manually
  102
+    activate and deactivate it in your :meth:`~BaseCommand.handle` or
  103
+    :meth:`~NoArgsCommand.handle_noargs` method using the functions provided by
  104
+    the I18N support code:
  105
+
  106
+    .. code-block:: python
  107
+
  108
+        from django.core.management.base import BaseCommand, CommandError
  109
+        from django.utils import translation
  110
+
  111
+        class Command(BaseCommand):
  112
+            ...
  113
+            self.can_import_settings = True
  114
+
  115
+            def handle(self, *args, **options):
  116
+
  117
+                # Activate a fixed locale, e.g. Russian
  118
+                translation.activate('ru')
  119
+
  120
+                # Or you can activate the LANGUAGE_CODE
  121
+                # chosen in the settings:
  122
+                #
  123
+                #from django.conf import settings
  124
+                #translation.activate(settings.LANGUAGE_CODE)
  125
+
  126
+                # Your command logic here
  127
+                # ...
  128
+
  129
+                translation.deactivate()
  130
+
  131
+    Take into account though, that system management commands typically have to
  132
+    be very careful about running in non-uniform locales, so:
  133
+
  134
+    * Make sure the :setting:`USE_I18N` setting is always ``True`` when running
  135
+      the command (this is one good example of the potential problems stemming
  136
+      from a dynamic runtime environment that Django commands avoid offhand by
  137
+      always using a fixed locale).
  138
+
  139
+    * Review the code of your command and the code it calls for behavioral
  140
+      differences when locales are changed and evaluate its impact on
  141
+      predictable behavior of your command.
  142
+
94 143
 Command objects
95 144
 ===============
96 145
 

0 notes on commit 0b0e945

Please sign in to comment.
Something went wrong with that request. Please try again.