Skip to content

Fall back to English when a translation catalog is corrupted - #120

Merged
PierreRaybaut merged 1 commit into
masterfrom
hotfix/corrupted-mo-fallback
Oct 29, 2026
Merged

PierreRaybaut merged 1 commit into
masterfrom
hotfix/corrupted-mo-fallback

Conversation

@PierreRaybaut

Copy link
Copy Markdown
Collaborator

Problem

guidata.configtools.get_translation calls gettext.translation(modname, modlocpath, fallback=True). fallback=True only handles missing catalogs: a .mo file that exists but is corrupted or unreadable raises OSError (bad magic number, file is corrupt, permission denied), struct.error (truncated file), ValueError (invalid UTF-8, invalid Plural-Forms) or LookupError (unknown charset).

As get_translation runs at import time (guidata.config, plotpy.config, sigima.config, datalab.config), a single corrupted catalog prevents every guidata-based application from starting.

Fix

  • get_translation catches these errors, emits a RuntimeWarning naming the module, the locale directory and the underlying error, and returns the untranslated (English) callback (gettext.NullTranslations).
  • The exception tuple is explicit (no bare Exception) so that programming errors are not hidden.
  • gettext only caches successfully parsed catalogs, so no stale cache entry is left behind.

Tests

guidata/tests/unit/test_translations.py (Qt-free) builds GNU .mo catalogs in a temporary locale directory:

  • 7 corrupted cases (bad magic number, empty file, truncated header, offsets past end of file, unknown charset, invalid UTF-8 translation, invalid Plural-Forms): RuntimeWarning + English fallback. All 7 fail without the fix.
  • Valid catalog: translated, no warning.
  • Missing catalog: English, no warning (unchanged behavior).

Validation:

  • pytest (full guidata suite, Python 3.11): 251 passed, 2 skipped.
  • ruff format / ruff check: clean.
  • End-to-end smoke test: with a corrupted guidata fr catalog, guidata, plotpy.config, sigima.config and datalab.config all import; guidata falls back to English with the warning while DataLab's own catalog keeps working.

Release notes

Entry added to the Unreleased section of doc/release_notes/release_3.16.md (patch 3.16.x). To be cherry-picked into develop after merge.

Fixes #119

gettext.translation(..., fallback=True) only handles missing catalogs:
a corrupted or unreadable .mo raised OSError/struct.error/ValueError/
LookupError at import time and prevented guidata-based applications
(PlotPy, Sigima, DataLab) from starting. get_translation now emits a
RuntimeWarning identifying the faulty catalog and returns the
untranslated (English) callback.

Fixes #119

Assisted-by: Claude Opus 5.5
@PierreRaybaut
PierreRaybaut merged commit 228309b into master Oct 29, 2026
9 checks passed
@PierreRaybaut
PierreRaybaut deleted the hotfix/corrupted-mo-fallback branch October 29, 2026 16:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Corrupted .mo translation file prevents application startup

1 participant