diff --git a/backend/handler/metadata/launchbox_handler/utils.py b/backend/handler/metadata/launchbox_handler/utils.py index cc64d4ca6..33c8a6444 100644 --- a/backend/handler/metadata/launchbox_handler/utils.py +++ b/backend/handler/metadata/launchbox_handler/utils.py @@ -57,10 +57,22 @@ def fold_title(title: str) -> str: Letters of every script survive. Keeping only ASCII would reduce a title written in one to whatever digits it carries, collapsing "三國立志傳2" and "忍者村大战2" onto the same key. + + A mark only goes when it sits on a Latin letter, where it is an accent the + two sides may disagree about ("Astérix" against "Asterix"). Elsewhere it + spells the word: Devanagari and Thai build syllables from marks, so dropping + them would reduce "हिन्दी" to "हनद" and collide titles that differ. """ - decomposed = unicodedata.normalize("NFKD", title.casefold()) - without_accents = "".join(c for c in decomposed if not unicodedata.combining(c)) - return "".join(c for c in without_accents if c.isalnum()) + kept: list[str] = [] + for char in unicodedata.normalize("NFKD", title.casefold()): + if unicodedata.category(char).startswith("M"): + if kept and "a" <= kept[-1] <= "z": + continue + kept.append(char) + elif char.isalnum(): + kept.append(char) + + return "".join(kept) def file_name_forms(file_name: str) -> list[str]: diff --git a/backend/tasks/scheduled/update_launchbox_metadata.py b/backend/tasks/scheduled/update_launchbox_metadata.py index 98b0a3877..cbf957fb9 100644 --- a/backend/tasks/scheduled/update_launchbox_metadata.py +++ b/backend/tasks/scheduled/update_launchbox_metadata.py @@ -193,21 +193,23 @@ class UpdateLaunchboxMetadataTask(RemoteFilePullTask): and platform_elem is not None and platform_elem.text ): + platform_name = platform_elem.text.strip() + game = _element_to_dict(elem) + # Use a unique combination of name and platform as the key await writer.hset( LAUNCHBOX_METADATA_NAME_KEY, f"{name_elem.text.strip().lower()}" - f":{platform_elem.text.strip()}", - _element_to_dict(elem), + f":{platform_name}", + game, ) folded = fold_title(name_elem.text) if folded: await writer.hset( LAUNCHBOX_METADATA_FOLDED_NAME_KEY, - f"{folded}" - f":{platform_elem.text.strip()}", - _element_to_dict(elem), + f"{folded}:{platform_name}", + game, ) elif elem.tag == "GameAlternateName": diff --git a/backend/tests/handler/metadata/test_launchbox_handler.py b/backend/tests/handler/metadata/test_launchbox_handler.py index 3f94e5f62..b9d1f6057 100644 --- a/backend/tests/handler/metadata/test_launchbox_handler.py +++ b/backend/tests/handler/metadata/test_launchbox_handler.py @@ -713,6 +713,10 @@ class TestRemoteSourceGetRom: # its digits would collide with every other title numbered alike. ("三國立志傳2", "三國立志傳2"), ("Зона 51", "зона51"), + # Scripts that build syllables from marks keep them; stripping to + # alphanumerics alone would spell these titles differently. + ("हिन्दी गेम", "हिन्दीगेम"), + ("สนุกเกอร์", "สนุกเกอร์"), # Nothing comparable left, so callers must treat it as no key. (":: -- ::", ""), ],