refactor: improve XLIFF file handling in translation script

- Enhanced the `find_xliff_files` function to preserve the order of locales when mapping Crowdin locales to XLIFF paths.
- Updated the logic to return all available locales when no specific locales are provided, ensuring better handling of XLIFF files.
- Simplified the iteration over XLIFF files by removing unnecessary sorting in the final loop.
This commit is contained in:
BruceChen 2026-04-07 00:50:24 +08:00
parent 47f13a815c
commit 5dde778b53

View file

@ -640,16 +640,22 @@ def load_existing_translated_ids(xliff_path: Path) -> set[str]:
# ---------------------------------------------------------------------------
def find_xliff_files(bundle_dir: Path, locales: list[str] | None) -> dict[str, Path]:
"""Map Crowdin locale -> XLIFF path for the given bundle directory."""
result: dict[str, Path] = {}
"""Map Crowdin locale -> XLIFF path, preserving the order of *locales*.
When locales is None (all languages) files are ordered by filename.
"""
available: dict[str, Path] = {}
for xliff_path in sorted(bundle_dir.glob("*.xliff")):
name = xliff_path.stem
for locale in LANGUAGE_MAP:
if name.endswith(f"_{locale}"):
if locales is None or locale in locales:
result[locale] = xliff_path
available[locale] = xliff_path
break
return result
if locales is None:
return available
return {loc: available[loc] for loc in locales if loc in available}
def process_language(
@ -906,7 +912,7 @@ def main() -> None:
log.info("Found %d language(s) to process: %s",
len(xliff_files), ", ".join(sorted(xliff_files)))
for locale, xliff_path in sorted(xliff_files.items()):
for locale, xliff_path in xliff_files.items():
try:
process_language(
locale=locale,