mirror of
https://github.com/Hopiu/django-modeltranslation.git
synced 2026-05-27 21:44:00 +00:00
Added MODELTRANSLATION_PREPOPULATE_LANGUAGE setting to force prepopulated fields (slugs) to use a specific language as their source
This commit is contained in:
parent
cc5687e4d0
commit
b9d8359306
2 changed files with 11 additions and 3 deletions
|
|
@ -9,7 +9,7 @@ from django.contrib.contenttypes import generic
|
|||
# runs. The import is supposed to resolve a race condition between model import
|
||||
# and translation registration in production (see issue #19).
|
||||
import modeltranslation.models # NOQA
|
||||
from modeltranslation.settings import DEFAULT_LANGUAGE
|
||||
from modeltranslation.settings import DEFAULT_LANGUAGE, PREPOPULATE_LANGUAGE
|
||||
from modeltranslation.translator import translator
|
||||
from modeltranslation.utils import (
|
||||
get_translation_fields, build_css_class, build_localized_fieldname, get_language, unique)
|
||||
|
|
@ -130,8 +130,8 @@ class TranslationBaseModelAdmin(BaseModelAdmin):
|
|||
|
||||
def _patch_prepopulated_fields(self):
|
||||
if self.prepopulated_fields:
|
||||
# TODO: Perhaps allow to configure which language the slug should be based on?
|
||||
lang = get_language()
|
||||
# Default to the active language, unless explicitly configured
|
||||
lang = PREPOPULATE_LANGUAGE or get_language()
|
||||
prepopulated_fields_new = dict(self.prepopulated_fields)
|
||||
translation_fields = []
|
||||
for k, v in self.prepopulated_fields.items():
|
||||
|
|
|
|||
|
|
@ -12,6 +12,14 @@ if DEFAULT_LANGUAGE and DEFAULT_LANGUAGE not in AVAILABLE_LANGUAGES:
|
|||
elif not DEFAULT_LANGUAGE:
|
||||
DEFAULT_LANGUAGE = AVAILABLE_LANGUAGES[0]
|
||||
|
||||
# Fixed base language for prepopulated fields (slugs)
|
||||
# (If not set, the current request language will be used)
|
||||
PREPOPULATE_LANGUAGE = getattr(settings,
|
||||
'MODELTRANSLATION_PREPOPULATE_LANGUAGE', None)
|
||||
if PREPOPULATE_LANGUAGE and PREPOPULATE_LANGUAGE not in AVAILABLE_LANGUAGES:
|
||||
raise ImproperlyConfigured('MODELTRANSLATION_PREPOPULATE_LANGUAGE '
|
||||
'not in LANGUAGES setting.')
|
||||
|
||||
# Load allowed CUSTOM_FIELDS from django settings
|
||||
CUSTOM_FIELDS = getattr(settings, 'MODELTRANSLATION_CUSTOM_FIELDS', ())
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue