diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 1980202..c459121 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -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) diff --git a/docs/modeltranslation/commands.rst b/docs/modeltranslation/commands.rst index d3becf5..531e9c8 100644 --- a/docs/modeltranslation/commands.rst +++ b/docs/modeltranslation/commands.rst @@ -76,3 +76,7 @@ Allowed modes are listed :ref:`here `. To choose ``False` If ``populate`` is not specified, then current auto-population mode is used. *Current* means the one set by :ref:`settings `. + +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``. diff --git a/docs/modeltranslation/installation.rst b/docs/modeltranslation/installation.rst index c23aa98..a05e265 100644 --- a/docs/modeltranslation/installation.rst +++ b/docs/modeltranslation/installation.rst @@ -318,3 +318,17 @@ Default: ``True`` .. versionadded:: 0.6 Control if :ref:`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. diff --git a/modeltranslation/management/commands/loaddata.py b/modeltranslation/management/commands/loaddata.py index 0ddb46f..5912bd0 100644 --- a/modeltranslation/management/commands/loaddata.py +++ b/modeltranslation/management/commands/loaddata.py @@ -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): diff --git a/modeltranslation/settings.py b/modeltranslation/settings.py index c51fb58..68b3997 100644 --- a/modeltranslation/settings.py +++ b/modeltranslation/settings.py @@ -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)