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

Fixing when using languages that require a hyphen #10

Open
wants to merge 3 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
5 changes: 2 additions & 3 deletions linguo/managers.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from django.db import models
from django.db.models.fields.related import RelatedField
from django.conf import settings
from django.utils.translation import get_language

from linguo.utils import get_real_field_name
from linguo.utils import get_real_field_name, get_current_language


def rewrite_lookup_key(model, lookup_key):
@@ -13,7 +12,7 @@ def rewrite_lookup_key(model, lookup_key):
# If we are doing a lookup on a translatable field, we want to rewrite it to the actual field name
# For example, we want to rewrite "name__startswith" to "name_fr__startswith"
if pieces[0] in model._meta.translatable_fields:
lookup_key = get_real_field_name(pieces[0], get_language().split('-')[0])
lookup_key = get_real_field_name(pieces[0], get_current_language())

remaining_lookup = '__'.join(pieces[1:])
if remaining_lookup:
8 changes: 4 additions & 4 deletions linguo/utils.py
Original file line number Diff line number Diff line change
@@ -11,15 +11,15 @@ def get_real_field_name(field_name, language):
if lang_code == get_normalized_language(settings.LANGUAGES[0][0]):
return field_name
else:
return '%s_%s' % (field_name, language)
return '%s_%s' % (field_name, lang_code)


def get_normalized_language(language_code):
"""
Returns the actual language extracted from the given language code
(ie. locale stripped off). For example, 'en-us' becomes 'en'.
Return normalized language (ie. just like locale). For example, 'en-us'
becomes 'en_us'.
"""
return language_code.split('-')[0]
return translation._trans.to_locale(language_code, to_lower=True)


def get_current_language():