Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fallback to default language if field is empty #5

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion linguo/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,18 @@ def generate_field_getter(cls, field):
# Property that masks the getter of a translatable field
def getter(self_reference):
attrname = '%s_%s' % (field, self_reference._language)
return getattr(self_reference, attrname)
translated_attr = getattr(self_reference, attrname)

# If empty, fall back to default language
if (not translated_attr and
self_reference._language != settings.LANGUAGE_CODE):
current_language = self_reference._language
self_reference.translate(settings.LANGUAGE_CODE)
default_attr = getattr(self_reference, field)
self_reference.translate(current_language)
return default_attr
else:
return translated_attr
return getter

@classmethod
Expand Down Expand Up @@ -218,3 +229,6 @@ def create_translation(self, language, **kwargs):

trans_obj.save()
return trans_obj

def translate(self, language):
self._language = language