Make loaddata command retain locale (close #151).

This commit is contained in:
Jacek Tomaszewski 2013-10-12 15:14:10 +02:00
parent 49c406d760
commit e78ef34808
5 changed files with 39 additions and 1 deletions

View file

@ -17,6 +17,9 @@
resolves issue #161)
ADDED: An auto-population option to the loaddata command.
(resolves issue #160)
ADDED: A MODELTRANSLATION_LOADDATA_RETAIN_LOCALE setting for loaddata
command to leave locale alone.
(resolves issue #151)
FIXED: Compatibility with Django 1.6 development version.
(resolves issue #169)

View file

@ -76,3 +76,7 @@ Allowed modes are listed :ref:`here <auto-population-modes>`. To choose ``False`
If ``populate`` is not specified, then current auto-population mode is used. *Current* means
the one set by :ref:`settings <settings-modeltranslation_auto_populate>`.
Moreover, this ``loaddata`` command version can override the nasty habit of changing locale to
`en-us`. By default, it will retain proper locale. To get back to old behaviour, set
:ref:`settings-modeltranslation_loaddata_retain_locale` to ``False``.

View file

@ -318,3 +318,17 @@ Default: ``True``
.. versionadded:: 0.6
Control if :ref:`fallback <fallback>` (both language and value) will occur.
.. _settings-modeltranslation_loaddata_retain_locale:
``MODELTRANSLATION_LOADDATA_RETAIN_LOCALE``
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Default: ``True``
.. versionadded:: 0.7
Control if the ``loaddata`` command should leave the settings-defined locale alone. Setting it
to ``False`` will result in previous behaviour of ``loaddata``: inserting fixtures to database
under `en-us` locale.

View file

@ -1,8 +1,11 @@
from __future__ import with_statement
from optparse import make_option, OptionValueError
from django import VERSION
from django.core.management.commands.loaddata import Command as LoadDataCommand
# Because this command is used (instead of default loaddata), then settings have been imported
# and we can safely import MT modules
from modeltranslation import settings as mt_settings
from modeltranslation.utils import auto_populate
@ -19,6 +22,8 @@ def check_mode(option, opt_str, value, parser):
class Command(LoadDataCommand):
leave_locale_alone = mt_settings.LOADDATA_RETAIN_LOCALE # Django 1.6
option_list = LoadDataCommand.option_list + (
make_option('--populate', action='callback', callback=check_mode, dest='populate',
type='string',
@ -26,7 +31,17 @@ class Command(LoadDataCommand):
'auto-population MODE. Allowed values are: %s' % ALLOWED_FOR_PRINT),
)
def __init__(self):
super(Command, self).__init__()
if mt_settings.LOADDATA_RETAIN_LOCALE and VERSION < (1, 6):
from django.utils import translation
self.locale = translation.get_language()
def handle(self, *fixture_labels, **options):
if self.can_import_settings and hasattr(self, 'locale'):
from django.utils import translation
translation.activate(self.locale)
mode = options.get('populate')
if mode is not None:
with auto_populate(mode):

View file

@ -51,3 +51,5 @@ for key, value in FALLBACK_LANGUAGES.items():
raise ImproperlyConfigured(
'MODELTRANSLATION_FALLBACK_LANGUAGES: "%s" not in LANGUAGES setting.' % lang)
ENABLE_FALLBACKS = getattr(settings, 'MODELTRANSLATION_ENABLE_FALLBACKS', True)
LOADDATA_RETAIN_LOCALE = getattr(settings, 'MODELTRANSLATION_LOADDATA_RETAIN_LOCALE', True)