Add support for LOCALIZED_FIELDS_FALLBACKS

This commit is contained in:
Swen Kooij 2017-06-26 13:27:11 +03:00
parent b10472d3e9
commit 16e23963cc

View File

@ -1,6 +1,5 @@
import collections import collections
from django.conf import settings from django.conf import settings
from django.utils import translation from django.utils import translation
@ -104,12 +103,18 @@ class LocalizedValue(dict):
back to the primary language if there's no value back to the primary language if there's no value
in the current language.""" in the current language."""
value = self.get(translation.get_language()) fallbacks = getattr(settings, 'LOCALIZED_FIELDS_FALLBACKS', {})
if not value: language = translation.get_language() or settings.LANGUAGE_CODE
value = self.get(settings.LANGUAGE_CODE) languages = fallbacks.get(language, [settings.LANGUAGE_CODE])[:]
languages.insert(0, language)
return value or '' for lang_code in languages:
value = self.get(lang_code)
if value:
return value or ''
return ''
def __eq__(self, other): def __eq__(self, other):
"""Compares :paramref:self to :paramref:other for """Compares :paramref:self to :paramref:other for