mirror of
https://github.com/rommapp/romm.git
synced 2026-11-29 18:11:16 +01:00
feat(metadata): read a fan translation's tag and target language (#4665)
* feat(metadata): read a fan translation's tag and target language A translated dump is playable in the language it was translated into, but the tag saying so was kept verbatim, so the rom was filed under no language at all and the filter drawer grew one throwaway value per patch: "T+Eng1.1_RPGe" and "T-Eng0.98_DeJap" are the same thing to a reader and two different facets to the gallery. The GoodTools, TOSEC and plain spellings now resolve to one "Translation" tag, and the language the patch targets joins the rom's languages, so a Japanese game translated into English answers a search for English and reaches an emulator as English. A translation into a language RomM cannot name is still tagged, and a tag that merely starts with "tr", like "Trainer", is left alone. The group and patch version the raw tag carries are dropped, since nothing reads them and keeping them is what splits the facet. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01S4mTJZ63Bvafv3stpiGsHC * feat(metadata): read the spaced form of a GoodTools translation tag "[T Fre]" separates with a space rather than a sign, and it is the spelling a real ScreenScraper dump carries: its entry for "Super Mario Bros. (W) [T Fre]" is flagged as a translation into French. The tag was read only as "T+Fre" or "T-Fre", so that dump came out untranslated and unlanguaged. A space carries no meaning of its own, unlike the sign, so this spelling counts only when the word after it names a language: "(T Rex)" stays an ordinary tag. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01S4mTJZ63Bvafv3stpiGsHC * style(metadata): trim the translation alias comment to two lines Greptile flagged it against the repo's comment discipline: four lines, and the product as the actor. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01S4mTJZ63Bvafv3stpiGsHC * feat(metadata): resolve the short forms of a translation tag's language "[T-En]" already resolved through the filename language vocabulary; the non-ISO abbreviations "Ge", "Sp", "Du", "Gr" and "Jp" did not, so those dumps were tagged without a language. Tests now cover both forms. Also trims three comments and four test docstrings Greptile flagged against the repo's comment discipline. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01S4mTJZ63Bvafv3stpiGsHC * fix(metadata): stop reading a hyphenated word as a translation "(T-Rex)" matched the GoodTools superseded form and became a Translation tag. A superseded patch carries its version and group, so require that, or a language the vocabulary names, before the bare "T-" form counts. "[T+Tha]" still tags, since no ordinary word takes that shape. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01S4mTJZ63Bvafv3stpiGsHC * style(metadata): trim the translation regex comment to two lines Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01S4mTJZ63Bvafv3stpiGsHC --------- Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -234,6 +234,42 @@ def normalize_language(tag: str) -> str | None:
|
||||
return _LANGUAGE_BY_ALIAS.get(tag.strip().lower())
|
||||
|
||||
|
||||
# Codes only a translation tag uses for its target language ("[T+Eng]",
|
||||
# "[T-Ge]"). Two-letter ISO codes are absent: normalize_language has those.
|
||||
_TRANSLATION_LANGUAGE_ALIASES = {
|
||||
"ara": "Arabic",
|
||||
"chi": "Chinese",
|
||||
"dan": "Danish",
|
||||
"du": "Dutch",
|
||||
"dut": "Dutch",
|
||||
"eng": "English",
|
||||
"fin": "Finnish",
|
||||
"fre": "French",
|
||||
"ge": "German",
|
||||
"ger": "German",
|
||||
"gr": "Greek",
|
||||
"gre": "Greek",
|
||||
"ita": "Italian",
|
||||
"jap": "Japanese",
|
||||
"jp": "Japanese",
|
||||
"kor": "Korean",
|
||||
"nor": "Norwegian",
|
||||
"pol": "Polish",
|
||||
"por": "Portuguese",
|
||||
"rus": "Russian",
|
||||
"ser": "Serbian",
|
||||
"sp": "Spanish",
|
||||
"spa": "Spanish",
|
||||
"swe": "Swedish",
|
||||
}
|
||||
|
||||
|
||||
def translation_language(code: str) -> str | None:
|
||||
"""Resolve the language a translation tag targets, or None."""
|
||||
tag = code.strip().lower()
|
||||
return normalize_language(tag) or _TRANSLATION_LANGUAGE_ALIASES.get(tag)
|
||||
|
||||
|
||||
# ISO-639-1 languages with no filename shortcode of their own. Provider-only,
|
||||
# because a filename tag reads by dumper convention instead: "(Tr)" marks a
|
||||
# translation, not Turkish.
|
||||
|
||||
@@ -69,6 +69,7 @@ from .base_handler import (
|
||||
FSHandler,
|
||||
normalize_language,
|
||||
normalize_region,
|
||||
translation_language,
|
||||
)
|
||||
|
||||
# PICO-8 cartridges are often stored as PNG files
|
||||
@@ -200,6 +201,20 @@ VERSION_TAG_REGEX = re.compile(r"^(?:version|ver|v)(?:[\s._-](.*)|([.\d].*))", r
|
||||
REGION_TAG_REGEX = re.compile(r"^reg[\s|-](.*)$", re.I)
|
||||
REVISION_TAG_REGEX = re.compile(r"^rev[\s|-](.*)$", re.I)
|
||||
|
||||
# A fan translation, as GoodTools ("[T+Eng1.1_RPGe]"), TOSEC ("[tr fr]") and
|
||||
# plainer sets ("(Translation)") write it. Anchored, so "Trainer" is not one.
|
||||
TRANSLATION_TAG_REGEX = re.compile(
|
||||
r"^(?:t(?:(?P<superseded>-)|\+)(?P<goodtools>[a-z]{2,3})(?P<patch>.*)"
|
||||
r"|t[\s_-]+(?P<spaced>[a-z]{2,3}).*"
|
||||
r"|tr(?:[\s_-]+(?P<tosec>[a-z]{2,3}))?"
|
||||
r"|translat(?:ed|ion))$",
|
||||
re.I,
|
||||
)
|
||||
|
||||
# The tag a translated dump carries, in place of the group and patch version
|
||||
# the raw tag encodes, which would give every translation its own facet value.
|
||||
TRANSLATION_TAG = "Translation"
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ParsedTags:
|
||||
@@ -406,6 +421,26 @@ class FSRomsHandler(FSHandler):
|
||||
languages.append(LANGUAGES_BY_SHORTCODE[raw_tag])
|
||||
continue
|
||||
|
||||
# Read before the language pass: a translated game is playable in
|
||||
# the language its tag names.
|
||||
translation_match = TRANSLATION_TAG_REGEX.match(raw_tag)
|
||||
if translation_match:
|
||||
spaced = translation_match["spaced"]
|
||||
code = spaced or translation_match["goodtools"]
|
||||
code = code or translation_match["tosec"]
|
||||
language = translation_language(code) if code else None
|
||||
# "T Rex" and a suffixless "T-Rex" collide with ordinary words,
|
||||
# so those two spellings only count when the code is a language.
|
||||
bare_superseded = (
|
||||
translation_match["superseded"] and not translation_match["patch"]
|
||||
)
|
||||
if language or not (spaced or bare_superseded):
|
||||
if TRANSLATION_TAG not in other_tags:
|
||||
other_tags.append(TRANSLATION_TAG)
|
||||
if language and language not in languages:
|
||||
languages.append(language)
|
||||
continue
|
||||
|
||||
# Region by name, alternate spelling, or differently-cased code.
|
||||
# Ahead of the equivalent language pass so a lowercased code that
|
||||
# both tables claim ("nl", "no") keeps reading as a region.
|
||||
|
||||
@@ -18,6 +18,7 @@ from handler.filesystem.base_handler import (
|
||||
provider_language_name,
|
||||
provider_region_name,
|
||||
region_ranks_for_priority,
|
||||
translation_language,
|
||||
)
|
||||
from models.base import FILE_NAME_MAX_LENGTH
|
||||
|
||||
@@ -647,6 +648,28 @@ class TestRegionRanksForPriority:
|
||||
assert region_ranks_for_priority([]) == {}
|
||||
|
||||
|
||||
class TestTranslationLanguage:
|
||||
@pytest.mark.parametrize(
|
||||
("code", "expected"),
|
||||
[
|
||||
("Eng", "English"),
|
||||
("En", "English"),
|
||||
("Ita", "Italian"),
|
||||
("It", "Italian"),
|
||||
("Ge", "German"),
|
||||
("Sp", "Spanish"),
|
||||
("Du", "Dutch"),
|
||||
("Gr", "Greek"),
|
||||
("Jp", "Japanese"),
|
||||
],
|
||||
)
|
||||
def test_resolves_the_forms_a_translation_tag_uses(self, code: str, expected: str):
|
||||
assert translation_language(code) == expected
|
||||
|
||||
def test_an_unnamed_language_resolves_to_none(self):
|
||||
assert translation_language("Tha") is None
|
||||
|
||||
|
||||
class TestNormalizeProviderRegions:
|
||||
"""Provider shortcodes collapse onto the names filename parsing produces."""
|
||||
|
||||
|
||||
@@ -303,6 +303,104 @@ class TestFSRomsHandler:
|
||||
assert handler.parse_tags("Game (nl).rom").regions == ["Netherlands"]
|
||||
assert handler.parse_tags("Game (no).rom").regions == ["Norway"]
|
||||
|
||||
def test_parse_tags_reads_a_goodtools_translation(self, handler: FSRomsHandler):
|
||||
"""A fan translation is playable in the language it targets, so the tag
|
||||
names the language as well as marking the dump."""
|
||||
parsed = handler.parse_tags("Final Fantasy V (Japan) [T+Eng1.1_RPGe].sfc")
|
||||
|
||||
assert parsed.regions == ["Japan"]
|
||||
assert parsed.languages == ["English"]
|
||||
assert parsed.other_tags == ["Translation"]
|
||||
|
||||
def test_parse_tags_reads_a_superseded_translation(self, handler: FSRomsHandler):
|
||||
"""GoodTools marks an older patch "T-", which is still a translation."""
|
||||
parsed = handler.parse_tags("Bahamut Lagoon (Japan) [T-Eng0.98_DeJap].sfc")
|
||||
|
||||
assert parsed.languages == ["English"]
|
||||
assert parsed.other_tags == ["Translation"]
|
||||
|
||||
def test_parse_tags_reads_a_two_letter_translation(self, handler: FSRomsHandler):
|
||||
"""The code may be the two-letter form, "[T-En]" for "[T-Eng]"."""
|
||||
parsed = handler.parse_tags("Game (Japan) [T-En].sfc")
|
||||
|
||||
assert parsed.languages == ["English"]
|
||||
assert parsed.other_tags == ["Translation"]
|
||||
|
||||
def test_parse_tags_reads_a_non_iso_translation_code(self, handler: FSRomsHandler):
|
||||
"""Ge, Sp, Du, Gr and Jp are not ISO codes but do name a language."""
|
||||
parsed = handler.parse_tags("Game (Japan) [T+Ge].sfc")
|
||||
|
||||
assert parsed.languages == ["German"]
|
||||
assert parsed.other_tags == ["Translation"]
|
||||
|
||||
def test_parse_tags_leaves_a_hyphenated_word_alone(self, handler: FSRomsHandler):
|
||||
"""A suffixless "T-" tag has to name a language, so "T-Rex" is a tag."""
|
||||
parsed = handler.parse_tags("Game (USA) (T-Rex).nes")
|
||||
|
||||
assert parsed.other_tags == ["T-Rex"]
|
||||
assert parsed.languages == []
|
||||
|
||||
def test_parse_tags_reads_a_superseded_patch_of_an_unnamed_language(
|
||||
self, handler: FSRomsHandler
|
||||
):
|
||||
"""Carrying a patch version marks it as GoodTools', not a stray word."""
|
||||
parsed = handler.parse_tags("Game (Japan) [T-Tha1.0_Grp].gba")
|
||||
|
||||
assert parsed.other_tags == ["Translation"]
|
||||
assert parsed.languages == []
|
||||
|
||||
def test_parse_tags_reads_a_spaced_translation(self, handler: FSRomsHandler):
|
||||
"""GoodTools also separates with a space, as ScreenScraper spells it."""
|
||||
parsed = handler.parse_tags("Super Mario Bros. (W) [T Fre].nes")
|
||||
|
||||
assert parsed.languages == ["French"]
|
||||
assert parsed.other_tags == ["Translation"]
|
||||
|
||||
def test_parse_tags_spaced_form_needs_a_language(self, handler: FSRomsHandler):
|
||||
"""The word after a spaced "T" has to name a language."""
|
||||
parsed = handler.parse_tags("Game (USA) (T Rex).nes")
|
||||
|
||||
assert parsed.other_tags == ["T Rex"]
|
||||
assert parsed.languages == []
|
||||
|
||||
def test_parse_tags_reads_a_tosec_translation(self, handler: FSRomsHandler):
|
||||
parsed = handler.parse_tags("Game (1994)(Konami)(JP)[tr fr].tap")
|
||||
|
||||
assert parsed.languages == ["French"]
|
||||
assert "Translation" in parsed.other_tags
|
||||
|
||||
def test_parse_tags_translation_without_a_language(self, handler: FSRomsHandler):
|
||||
""" "(Tr)" names no language, so the dump is marked and nothing more."""
|
||||
parsed = handler.parse_tags("Game (Japan) (Tr).md")
|
||||
|
||||
assert parsed.languages == []
|
||||
assert parsed.other_tags == ["Translation"]
|
||||
|
||||
def test_parse_tags_translation_does_not_repeat_a_language(
|
||||
self, handler: FSRomsHandler
|
||||
):
|
||||
"""A file carrying both spellings is filed under the language once."""
|
||||
parsed = handler.parse_tags("Seiken Densetsu 3 (Japan) (En) (Translation).sfc")
|
||||
|
||||
assert parsed.languages == ["English"]
|
||||
assert parsed.other_tags == ["Translation"]
|
||||
|
||||
def test_parse_tags_leaves_other_tr_tags_alone(self, handler: FSRomsHandler):
|
||||
"""A tag merely starting with "tr" is not a translation."""
|
||||
parsed = handler.parse_tags("Game (USA) (Trainer).nes")
|
||||
|
||||
assert parsed.other_tags == ["Trainer"]
|
||||
assert parsed.languages == []
|
||||
|
||||
def test_parse_tags_translation_into_an_unnamed_language(
|
||||
self, handler: FSRomsHandler
|
||||
):
|
||||
"""An unnamed target language still marks the dump as translated."""
|
||||
parsed = handler.parse_tags("Game (Japan) [T+Tha].gba")
|
||||
|
||||
assert parsed.languages == []
|
||||
assert parsed.other_tags == ["Translation"]
|
||||
|
||||
def test_parse_tags_reads_two_letter_region_codes(self, handler: FSRomsHandler):
|
||||
"""TOSEC and similar sets write the provider shortcode, not the GoodTools
|
||||
one, so "(US)" has to read as a region rather than land in tags."""
|
||||
@@ -329,7 +427,7 @@ class TestFSRomsHandler:
|
||||
# "(Tr)" marks a translation by dumper convention, never Turkey.
|
||||
parsed = handler.parse_tags("Game (Japan) (Tr).rom")
|
||||
assert parsed.regions == ["Japan"]
|
||||
assert parsed.other_tags == ["Tr"]
|
||||
assert parsed.other_tags == ["Translation"]
|
||||
|
||||
def test_parse_tags_language_casing_is_normalized(self, handler: FSRomsHandler):
|
||||
"""Language names collapse to one canonical spelling regardless of casing."""
|
||||
|
||||
Reference in New Issue
Block a user