From cfe61a26d06dd9fe50f18467f393ea11658231b8 Mon Sep 17 00:00:00 2001
From: Daniel Edwards <daniel.edwards@iress.com>
Date: Tue, 19 Mar 2024 10:40:02 +1100
Subject: [PATCH] Refactor get_language_name function to handle missing
 language names

---
 cps/isoLanguages.py | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)

diff --git a/cps/isoLanguages.py b/cps/isoLanguages.py
index 31e3dade..a66235ce 100644
--- a/cps/isoLanguages.py
+++ b/cps/isoLanguages.py
@@ -49,15 +49,24 @@ except ImportError:
 
 
 def get_language_names(locale):
-    return _LANGUAGE_NAMES.get(str(locale))
+    names = _LANGUAGE_NAMES.get(str(locale))
+    if names is None:
+        names = _LANGUAGE_NAMES.get(locale.language)
+    return names
 
 
 def get_language_name(locale, lang_code):
-    try:
-        return get_language_names(locale)[lang_code]
-    except KeyError:
-        log.error('Missing translation for language name: {}'.format(lang_code))
-        return "Unknown"
+    UNKNOWN_TRANSLATION = "Unknown"
+    names = get_language_names(locale)
+    if names is None:
+        log.error(f"Missing language names for locale: {str(locale)}/{locale.language}")
+        return UNKNOWN_TRANSLATION
+
+    name = names.get(lang_code, UNKNOWN_TRANSLATION)
+    if name == UNKNOWN_TRANSLATION:
+        log.error("Missing translation for language name: {}".format(lang_code))
+
+    return name
 
 
 def get_language_codes(locale, language_names, remainder=None):