Skip to content

MOBI: do not take the region of a different language in mobi2iana - #3453

Merged
kovidgoyal merged 1 commit into
kovidgoyal:masterfrom
ChandraChaan:fix/mobi2iana-region-pr
Nov 5, 2026
Merged

kovidgoyal merged 1 commit into
kovidgoyal:masterfrom
ChandraChaan:fix/mobi2iana-region-pr

Conversation

@ChandraChaan

Copy link
Copy Markdown
Contributor

mobi2iana() sets the region suffix whenever any language's entry in IANA_MOBI has the requested sub language number, not only the entries of the requested language:

if cc == langcode:
    prefix = code
if cl == sublangcode:
    suffix = subcode.lower() if subcode else None
    break

If the requested language has no entry for that sub language, suffix keeps the value from an earlier language. For example mobi2iana(7, 4) returns 'de-sa' (German with the Saudi Arabia region from Arabic's (1, 4)) instead of 'de', and (25, 8) gives 'ru-pt', (26, 12) 'hr-ca'. This moves the sub language check inside the language match. Entries that exist are unchanged: (9, 4) is still 'en-us', (7, 8) 'de-ch', (4, 12) 'zh-hk'.

I added test_mobi2iana to TestLocaleNames in src/calibre/utils/localization_test.py, since that module's find_tests is already registered.

I did not touch the caller in mobi/reader/headers.py, which passes the plain sub language while the table stores it shifted by two bits. That looks like a separate question.

I couldn't run calibre locally, so I ran the CI on a fork:

  • Test only, without the fix: CI fails on ubuntu and Arch with AssertionError: 'de-sa' != 'de' / Failed for: 7, 4 in test_mobi2iana (OK: 969 FAIL: 1). The macOS and Windows jobs of that run were cancelled after the failure.
  • Test with the fix: CI passes on ubuntu, macOS, Windows and Arch.

The commit in this PR is the squashed form of the fix branch (identical tree).

mobi2iana() set the region suffix whenever any language's entry had the
requested sub language number, not only entries of the requested
language. A language with no entry for that sub language therefore kept
the region of an earlier language, for example mobi2iana(7, 4) returned
'de-sa' (German, with the Saudi Arabia region of Arabic) instead of 'de'.

Only look for the sub language among the entries of the matching
language, and add a test.
@kovidgoyal
kovidgoyal merged commit 2da710c into kovidgoyal:master Nov 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants