Skip to content

Choose the interface language for each request - #6485

Open
Globgor wants to merge 5 commits into
solgenomics:masterfrom
Globgor:i18n/pr/01-choose-language
Open

Globgor wants to merge 5 commits into
solgenomics:masterfrom
Globgor:i18n/pr/01-choose-language

Conversation

@Globgor

@Globgor Globgor commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Description

First step towards an interface in the visitor's language. Nothing changes unless i18n_languages is set in the config: every request stays English, and libintl-perl is not needed.

  • New role SGN::Role::Site::I18N. For each request it takes the language from ?lang=, the lang cookie or the Accept-Language header (languages with q=0 are skipped), switches gettext (text domain sgn) to it, and goes back to English at the end of the request, also when the request dies.
  • When libintl-perl is installed, gettext returns Perl characters in every mode, English included, so a translated text with a non-ASCII letter is escaped once on the page.
  • libintl-perl (Locale::Messages) is loaded only when i18n_languages is set. A wrong setup (the library, a catalog or a system locale missing for a listed language) stops the server at start, with a message that names the problem.
  • Build.PL lists Locale::TextDomain. Lint checks the whole changed file, so I added use strict; use warnings; there.

About solgenomics/breedbase_dockerfile#63. This PR runs on the current image without libintl-perl. The next PRs of the chain (texts in Mason templates) need the library in the image, so they wait for #63.

Tests in breedbase/breedbase:latest: without libintl-perl the whole prove --recurse t/unit passes (38 files, 1594 tests; I18N.t is skipped, I18N_off.t checks that the site starts and that a configured language stops the start with a clear message). With libintl-perl installed it passes too (1601 tests); the checks for q=0 and for a request that dies fail on the first commit.

Next in the chain: Mason components that can translate text.

Part of #5915.

Checklist

  • New feature
    • Relevant tests have been created and run.
    • Any new Perl has been documented using perldoc.

A new site role reads i18n_languages from the configuration and, for
each request, picks the language from ?lang=, the lang cookie or the
Accept-Language header. It switches gettext (Locale::TextDomain, text
domain sgn) to that language and back to English after the request.
Without i18n_languages nothing changes.

AI-Tool: Claude Code
Accept-Language entries with q=0 mean "not this language"; they were
still chosen when nothing else matched ("uk;q=0, de" gave Ukrainian).
They are now dropped before the language is detected.

The language went back to English in "after finalize", which does not run
when finalize dies. The reset now wraps handle_request with Try::Tiny's
finally, so every request ends in English whatever happens in it.

AI-Tool: Claude Code
The role loaded Locale::Messages at compile time, so SGN did not start
without libintl-perl even when i18n_languages was unset. It now loads it
in setup_finalize, only when languages are configured, and stops the
start with a clear message when the library is missing. The role test
skips without the library; a new test hides it and checks both cases.

AI-Tool: Claude Code
Without i18n_languages the role did not set the text domain's codeset and
UTF-8 filter, so gettext gave the English text back as bytes. A wrapped
text with a non-ASCII letter (a no-break space, for example) was then
escaped byte by byte: "( " became "( " on the page.
When libintl-perl is installed, the filter is now set in every mode.

AI-Tool: Claude Code

This branch has not been deployed

No deployments
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.

1 participant