Skip to content

Commit ef095e4

Browse files
fix: better normalization of language codes
1 parent d7fd132 commit ef095e4

1 file changed

Lines changed: 18 additions & 4 deletions

File tree

openedx/core/djangoapps/content/course_overviews/migrations/0030_backfill_new_catalog_courseruns.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@
1111

1212
log = logging.getLogger(__name__)
1313

14+
# https://github.com/openedx/openedx-platform/issues/38036
15+
NORMALIZE_LANGUAGE_CODES = {
16+
"zh-hans": "zh-cn",
17+
"zh-hant": "zh-hk",
18+
"ca@valencia": "ca-es-valencia",
19+
}
20+
1421

1522
def backfill_openedx_catalog(apps, schema_editor):
1623
"""
@@ -59,12 +66,19 @@ def backfill_openedx_catalog(apps, schema_editor):
5966
display_name: str = (course_overview.display_name if course_overview else None) or course_code
6067

6168
# Determine the course language.
69+
# Note that in Studio, the options for course language generally came from the ALL_LANGUAGES setting, which is
70+
# mostly two-letter language codes with no locale, except it uses "zh_HANS" for Mandarin and "zh_HANT" for
71+
# Cantonese. We normalize those to "zh-cn" and "zh-hk" for consistency with our platform UI languages /
72+
# Transifex, but you can still access the "old" version using the CatalogCourse.language_short
73+
# getter/setter for backwards compatbility. See https://github.com/openedx/openedx-platform/issues/38036
6274
language = settings.LANGUAGE_CODE
6375
if course_overview and course_overview.language:
6476
language = course_overview.language.lower()
65-
if len(language) > 2 and language[2] == "_":
66-
language[2] = "-" # Ensure we use hyphens for consistency (`en-us` not `en_us`)
67-
if len(language) > 2 and language[2] not in ("-", "@"):
77+
language = language.replace("_", "-") # Ensure we use hyphens for consistency (`en-us` not `en_us`)
78+
# Normalize this language code. The previous/non-normalized code will still be available via the
79+
# "language_short" property for backwards compatibility.
80+
language = NORMALIZE_LANGUAGE_CODES.get(language, language)
81+
if len(language) > 2 and language[2] != "-":
6882
# This seems like an invalid value; revert to the default:
6983
log.warning(
7084
'The course with ID "%s" has invalid language "%s" - using default language "%s" instead.',
@@ -102,7 +116,7 @@ def backfill_openedx_catalog(apps, schema_editor):
102116
raise ValueError(
103117
f"The course {course_run.course_id} exists in modulestore with a different capitalization of its "
104118
f'course code compared to other instances of the same run ("{course_code}" vs "{cc.course_code}"). '
105-
'This really should not happen. To fix it, delete the inconsistent course runs (!). '
119+
"This really should not happen. To fix it, delete the inconsistent course runs (!). "
106120
)
107121

108122
# Create the CourseRun

0 commit comments

Comments
 (0)