Skip to content

Commit

Permalink
[Feature] Improve legal code language selection/fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
manulthanura committed Oct 13, 2024
1 parent 704521b commit ca5937a
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 46 deletions.
16 changes: 8 additions & 8 deletions i18n/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
# First-party/Local
from i18n.utils import (
active_translation,
get_default_language_for_jurisdiction_deed,
get_default_language_for_jurisdiction_naive,
get_default_language_for_jurisdiction_deed_ux,
get_default_language_for_jurisdiction_legal_code,
get_jurisdiction_name,
get_pofile_creation_date,
get_pofile_path,
Expand Down Expand Up @@ -164,29 +164,29 @@ class I18NTest(TestCase):
def test_get_language_for_jurisdiction_deed(self):
# "be" jurisdiction default is "fr"
self.assertEqual(
"fr", get_default_language_for_jurisdiction_deed("be")
"fr", get_default_language_for_jurisdiction_deed_ux("be")
)
# "am" jurisdiction default is "hy"
# the "hy" translation is incomplete so we return the global default
# https://github.com/creativecommons/cc-legal-tools-app/issues/444
self.assertEqual(
"en", get_default_language_for_jurisdiction_deed("am")
"en", get_default_language_for_jurisdiction_deed_ux("am")
)
# "xx" is an invalid jurisdiction
# return global default ("en")
self.assertEqual(
"en", get_default_language_for_jurisdiction_deed("xx")
"en", get_default_language_for_jurisdiction_deed_ux("xx")
)

def test_get_language_for_jurisdiction_legal_code(self):
# "be" jurisdiction default is "fr"
self.assertEqual(
"fr", get_default_language_for_jurisdiction_naive("be")
"fr", get_default_language_for_jurisdiction_legal_code("be")
)
# "xx" is an invalid jurisdiction
# return global default ("en")
self.assertEqual(
"en", get_default_language_for_jurisdiction_naive("xx")
"en", get_default_language_for_jurisdiction_legal_code("xx")
)


Expand Down Expand Up @@ -471,4 +471,4 @@ def test_map_legacy_to_django_language_code(self):
self.assertEqual("sr-latn", transifex_code)

transifex_code = map_legacy_to_django_language_code("zh_Hans")
self.assertEqual("zh-hans", transifex_code)
self.assertEqual("zh-hans", transifex_code)
35 changes: 23 additions & 12 deletions i18n/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ def get_translation_object(

# Add a fallback to the standard Django translation for this language. This
# gets us the non-legal-code parts of the pages.
if language_code in settings.LANGUAGES_MOSTLY_TRANSLATED:
if language_code in settings.LANGUAGES_AVAILABLE_DEEDS_UX:
tool_translation_object.add_fallback(
translation.trans_real.translation(language_code)
)
elif language_default in settings.LANGUAGES_MOSTLY_TRANSLATED:
elif language_default in settings.LANGUAGES_AVAILABLE_DEEDS_UX:
tool_translation_object.add_fallback(
translation.trans_real.translation(language_default)
)
Expand Down Expand Up @@ -253,20 +253,30 @@ def map_legacy_to_django_language_code(legacy_language_code: str) -> str:
return django_language_code


def get_default_language_for_jurisdiction_deed(jurisdiction_code):
def get_default_language_for_jurisdiction_deed_ux(jurisdiction_code):
default_language = DEFAULT_JURISDICTION_LANGUAGES.get(
jurisdiction_code, settings.LANGUAGE_CODE
)
if default_language in settings.LANGUAGES_MOSTLY_TRANSLATED:
if default_language in settings.LANGUAGES_AVAILABLE_DEEDS_UX:
return default_language
else:
return settings.LANGUAGE_CODE


def get_default_language_for_jurisdiction_naive(jurisdiction_code):
return DEFAULT_JURISDICTION_LANGUAGES.get(
jurisdiction_code, settings.LANGUAGE_CODE
)
def get_default_language_for_jurisdiction_legal_code(jurisdiction_code):
default_language = DEFAULT_JURISDICTION_LANGUAGES.get(jurisdiction_code, settings.LANGUAGE_CODE)
if default_language in settings.LANGUAGES_AVAILABLE_LEGAL_CODE:
return default_language
else:
return settings.LANGUAGE_CODE


def load_legal_code_translations(jurisdiction_code):
settings.LANGUAGES_AVAILABLE_LEGAL_CODE = {
# Populate this dictionary with the available translations for legal code
"en": "English",
"fr": "French",
}


def get_jurisdiction_name(category, unit, version, jurisdiction_code):
Expand Down Expand Up @@ -310,7 +320,8 @@ def load_deeds_ux_translations():
that meet or exceed the TRANSLATION_THRESHOLD).
"""
deeds_ux_po_file_info = {}
languages_mostly_translated = []
# languages_mostly_translated = []
languages_available_deeds_ux = []
for language_code, pofile_path in get_deeds_ux_pofiles():
pofile_obj = polib.pofile(pofile_path)
percent_translated = pofile_obj.percent_translated()
Expand All @@ -326,12 +337,12 @@ def load_deeds_ux_translations():
and language_code != settings.LANGUAGE_CODE
):
continue
languages_mostly_translated.append(language_code)
languages_available_deeds_ux.append(language_code)
deeds_ux_po_file_info = dict(sorted(deeds_ux_po_file_info.items()))
# Add global settings
settings.DEEDS_UX_PO_FILE_INFO = deeds_ux_po_file_info
settings.LANGUAGES_MOSTLY_TRANSLATED = sorted(
list(set(languages_mostly_translated))
settings.LANGUAGES_AVAILABLE_DEEDS_UX = sorted(
list(set(languages_available_deeds_ux))
)


Expand Down
12 changes: 6 additions & 6 deletions legal_tools/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
# First-party/Local
from i18n import LANGMAP_DJANGO_TO_PCRE
from i18n.utils import (
get_default_language_for_jurisdiction_deed,
get_default_language_for_jurisdiction_naive,
get_default_language_for_jurisdiction_deed_ux,
get_default_language_for_jurisdiction_legal_code,
get_jurisdiction_name,
get_pofile_path,
get_translation_object,
Expand Down Expand Up @@ -249,7 +249,7 @@ def get_publish_files(self):
language_code = self.language_code
tool = self.tool
juris_code = tool.jurisdiction_code
language_default = get_default_language_for_jurisdiction_naive(
language_default = get_default_language_for_jurisdiction_legal_code(
juris_code
)
filename = f"legalcode.{self.language_code}.html"
Expand Down Expand Up @@ -348,7 +348,7 @@ def translation_domain(self):
return self.tool.resource_slug

def get_translation_object(self):
language_default = get_default_language_for_jurisdiction_naive(
language_default = get_default_language_for_jurisdiction_legal_code(
self.tool.jurisdiction_code
)
return get_translation_object(
Expand Down Expand Up @@ -554,7 +554,7 @@ def get_metadata(self):
"""
Return a dictionary with the metadata for this tool.
"""
language_default = get_default_language_for_jurisdiction_deed(
language_default = get_default_language_for_jurisdiction_deed_ux(
self.jurisdiction_code
)
data = {}
Expand Down Expand Up @@ -601,7 +601,7 @@ def get_publish_files(self, language_code):
correctly
"""
juris_code = self.jurisdiction_code
language_default = get_default_language_for_jurisdiction_deed(
language_default = get_default_language_for_jurisdiction_deed_ux(
juris_code
)
filename = f"deed.{language_code}.html"
Expand Down
13 changes: 13 additions & 0 deletions legal_tools/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -628,3 +628,16 @@ def test_update_titles_with_updates(self):
self.assertEqual(
{"records_updated": 4, "records_requiring_update": 0}, results
)
class UpdateLangInfoTest(TestCase):
def test_update_lang_info(self):
language_code = "fr"
utils.update_lang_info(language_code)
self.assertIn(language_code, LANG_INFO)
self.assertEqual(LANG_INFO[language_code]["name"], "French")
self.assertEqual(LANG_INFO[language_code]["name_local"], "français")
self.assertFalse(LANG_INFO[language_code]["bidi"])

def test_update_lang_info_unknown_locale(self):
language_code = "unknown"
utils.update_lang_info(language_code)
self.assertNotIn(language_code, LANG_INFO)
6 changes: 3 additions & 3 deletions legal_tools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from i18n import UNIT_NAMES
from i18n.utils import (
active_translation,
get_default_language_for_jurisdiction_naive,
get_default_language_for_jurisdiction_legal_code,
get_jurisdiction_name,
get_translation_object,
map_legacy_to_django_language_code,
Expand Down Expand Up @@ -145,7 +145,7 @@ def parse_legal_code_filename(filename):
if jurisdiction:
language_code = (
language_code
or get_default_language_for_jurisdiction_naive(jurisdiction)
or get_default_language_for_jurisdiction_legal_code(jurisdiction)
)
else:
language_code = language_code or settings.LANGUAGE_CODE
Expand Down Expand Up @@ -511,7 +511,7 @@ def update_title(options):
# Translate title using legal code translation domain for legal
# code that is in Transifex (ex. CC0, Licenses 4.0)
slug = f"{unit}_{version}".replace(".", "")
language_default = get_default_language_for_jurisdiction_naive(
language_default = get_default_language_for_jurisdiction_legal_code(
jurisdiction
)
current_translation = get_translation_object(
Expand Down
34 changes: 17 additions & 17 deletions legal_tools/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
# First-party/Local
from i18n.utils import (
active_translation,
get_default_language_for_jurisdiction_deed,
get_default_language_for_jurisdiction_naive,
get_default_language_for_jurisdiction_deed_ux,
get_default_language_for_jurisdiction_legal_code,
get_jurisdiction_name,
load_deeds_ux_translations,
map_django_to_transifex_language_code,
Expand Down Expand Up @@ -87,7 +87,7 @@ def get_category_and_category_title(category=None, tool=None):
def get_languages_and_links_for_deeds_ux(request_path, selected_language_code):
languages_and_links = []

for language_code in settings.LANGUAGES_MOSTLY_TRANSLATED:
for language_code in settings.LANGUAGES_AVAILABLE_DEEDS_UX:
language_info = translation.get_language_info(language_code)
link = request_path.replace(
f".{selected_language_code}",
Expand Down Expand Up @@ -147,8 +147,8 @@ def get_deed_rel_path(
language_default,
):
deed_rel_path = os.path.relpath(deed_url, path_start)
if language_code not in settings.LANGUAGES_MOSTLY_TRANSLATED:
if language_default in settings.LANGUAGES_MOSTLY_TRANSLATED:
if language_code not in settings.LANGUAGES_AVAILABLE_DEEDS_UX:
if language_default in settings.LANGUAGES_AVAILABLE_DEEDS_UX:
# Translation incomplete, use region default language
deed_rel_path = deed_rel_path.replace(
f"deed.{language_code}", f"deed.{language_default}"
Expand All @@ -167,8 +167,8 @@ def get_list_paths(language_code, language_default):
f"/publicdomain/list.{language_code}",
]
for index, path in enumerate(paths):
if language_code not in settings.LANGUAGES_MOSTLY_TRANSLATED:
if language_default in settings.LANGUAGES_MOSTLY_TRANSLATED:
if language_code not in settings.LANGUAGES_AVAILABLE_DEEDS_UX:
if language_default in settings.LANGUAGES_AVAILABLE_DEEDS_UX:
# Translation incomplete, use region default language
paths[index] = path.replace(
f"/list.{language_code}", f"/list.{language_default}"
Expand Down Expand Up @@ -258,11 +258,11 @@ def name_local(legal_code):
def normalize_path_and_lang(request_path, jurisdiction, language_code):
if not language_code:
if "legalcode" in request_path:
language_code = get_default_language_for_jurisdiction_naive(
language_code = get_default_language_for_jurisdiction_legal_code(
jurisdiction
)
else:
language_code = get_default_language_for_jurisdiction_deed(
language_code = get_default_language_for_jurisdiction_deed_ux(
jurisdiction
)
if not request_path.endswith(f".{language_code}"):
Expand Down Expand Up @@ -377,7 +377,7 @@ def view_list(request, category, language_code=None):
request.path, language_code = normalize_path_and_lang(
request.path, None, language_code
)
if language_code not in settings.LANGUAGES_MOSTLY_TRANSLATED:
if language_code not in settings.LANGUAGES_AVAILABLE_DEEDS_UX:
raise Http404(f"invalid language: {language_code}")

translation.activate(language_code)
Expand All @@ -403,7 +403,7 @@ def view_list(request, category, language_code=None):
lc_unit = lc.tool.unit
lc_version = lc.tool.version
lc_identifier = lc.tool.identifier()
lc_language_default = get_default_language_for_jurisdiction_naive(
lc_language_default = get_default_language_for_jurisdiction_legal_code(
lc.tool.jurisdiction_code,
)
lc_lang_code = lc.language_code
Expand Down Expand Up @@ -497,13 +497,13 @@ def view_deed(
request.path, language_code = normalize_path_and_lang(
request.path, jurisdiction, language_code
)
if language_code not in settings.LANGUAGES_MOSTLY_TRANSLATED:
if language_code not in settings.LANGUAGES_AVAILABLE_DEEDS_UX:
return view_page_not_found(
request, Http404(f"invalid language: {language_code}")
)

path_start = os.path.dirname(request.path)
language_default = get_default_language_for_jurisdiction_deed(jurisdiction)
language_default = get_default_language_for_jurisdiction_deed_ux(jurisdiction)

try:
tool = Tool.objects.get(
Expand All @@ -521,7 +521,7 @@ def view_deed(
# Next, try to load legal code with default language for the
# jurisdiction
legal_code = tool.get_legal_code_for_language_code(
get_default_language_for_jurisdiction_naive(jurisdiction)
get_default_language_for_jurisdiction_legal_code(jurisdiction)
)
except LegalCode.DoesNotExist:
# Last, load legal code with global default language (English)
Expand Down Expand Up @@ -619,7 +619,7 @@ def view_legal_code(
request.path, language_code = normalize_path_and_lang(
request.path, jurisdiction, language_code
)
language_default = get_default_language_for_jurisdiction_naive(
language_default = get_default_language_for_jurisdiction_legal_code(
jurisdiction
)

Expand Down Expand Up @@ -647,9 +647,9 @@ def view_legal_code(
)

# Use Deeds & UX translations for title instead of Legal Code
if language_code in settings.LANGUAGES_MOSTLY_TRANSLATED:
if language_code in settings.LANGUAGES_AVAILABLE_DEEDS_UX:
translation.activate(language_code)
elif language_default in settings.LANGUAGES_MOSTLY_TRANSLATED:
elif language_default in settings.LANGUAGES_AVAILABLE_DEEDS_UX:
translation.activate(language_default)
else:
translation.activate(settings.LANGUAGE_CODE)
Expand Down

0 comments on commit ca5937a

Please sign in to comment.