Skip to content

Commit 070851d

Browse files
committed
fix: respect configured IGDB region priority
Generated-By: PostHog Code Task-Id: 6764d95b-659d-4e11-a5c5-28e65431d944
1 parent 68c0f4c commit 070851d

2 files changed

Lines changed: 30 additions & 6 deletions

File tree

backend/handler/metadata/igdb_handler.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -353,22 +353,32 @@ def derive_player_count(
353353

354354

355355
def get_igdb_preferred_locale(rom: Rom | None = None) -> str | None:
356-
"""Get IGDB locale, preferring the rom's own region tag when available.
356+
"""Get IGDB locale from the ROM's prioritized regions when available.
357357
358358
Maps region priority codes to IGDB's game_localizations region identifiers.
359-
Checks the rom's tagged regions first, then falls back to scan.priority.region.
359+
Prioritizes the ROM's tagged regions by scan.priority.region, then falls
360+
back to scan.priority.region.
360361
361362
Returns:
362363
IGDB region identifier (e.g., "ja-JP", "EU") or None for default
363364
"""
365+
config = cm.get_config()
366+
priority = config.SCAN_REGION_PRIORITY
367+
364368
if rom is not None and isinstance(rom.regions, list):
369+
rom_codes: list[str] = []
365370
for region_name in rom.regions:
366371
code = region_name_to_provider_shortcode(region_name)
367372
if code and code in REGION_TO_IGDB_LOCALE:
368-
return REGION_TO_IGDB_LOCALE[code]
373+
rom_codes.append(code)
369374

370-
config = cm.get_config()
371-
for region in config.SCAN_REGION_PRIORITY:
375+
rom_codes.sort(
376+
key=lambda code: priority.index(code) if code in priority else len(priority)
377+
)
378+
if rom_codes:
379+
return REGION_TO_IGDB_LOCALE[rom_codes[0]]
380+
381+
for region in priority:
372382
if region.lower() in REGION_TO_IGDB_LOCALE:
373383
return REGION_TO_IGDB_LOCALE[region.lower()]
374384

backend/tests/handler/metadata/test_igdb_handler.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Tests for the IGDB metadata handler."""
22

3-
from unittest.mock import AsyncMock, patch
3+
from unittest.mock import AsyncMock, MagicMock, patch
44

55
import pytest
66

@@ -13,6 +13,7 @@
1313
IGDBHandler,
1414
_build_platforms_where,
1515
_platform_igdb_ids_with_twin,
16+
get_igdb_preferred_locale,
1617
)
1718

1819
GENESIS_IGDB_ID = 29
@@ -62,6 +63,19 @@ def _make_game(
6263
}
6364

6465

66+
class TestGetIGDBPreferredLocale:
67+
def test_multi_region_rom_respects_user_priority(self):
68+
"""The configured priority wins over filename tag order."""
69+
rom = MagicMock()
70+
rom.regions = ["Japan", "USA"]
71+
config = MagicMock(SCAN_REGION_PRIORITY=["us", "jp"])
72+
73+
with patch("handler.metadata.igdb_handler.cm.get_config", return_value=config):
74+
locale = get_igdb_preferred_locale(rom)
75+
76+
assert locale is None
77+
78+
6579
class TestSearchRomGameTypeFilter:
6680
"""Tests for _search_rom game_type filtering."""
6781

0 commit comments

Comments
 (0)