canopy-portal Fluent i18n
Overview
For a portal developer adding a string or a locale: canopy-portal uses Project Fluent for applicant-facing copy. Per ADR-008, English and Spanish are the two day-one locales; a third is a drop-in directory.
The runtime is one type, LocaleManager in services/canopy-portal/src/i18n.rs. serve() in main.rs builds it once at startup (LocaleManager::new walks the locales directory and fails closed on a missing directory, an unparseable locale name or a malformed bundle) and mounts the Arc<LocaleManager> as an Axum Extension on the top-level router (.layer(axum::Extension(locale_manager))). Server-only: mod i18n and its fluent-bundle / intl-memoizer / unic-langid dependencies compile under the crate’s server cargo feature (services/canopy-portal/Cargo.toml); the WASM client never sees them. The tests need server and preview, and every lane that builds them passes both explicitly (in BATTERY_FEATURES, xtask/src/lanes.rs, #1988); a test build without them stops at a compile_error! in main.rs (Testing).
The layer is loaded at startup but not yet consumed by any page — pages render hard-coded English today. Every Route variant in routes.rs renders a Dioxus component under src/pages/ that emits English inline, and csp.rs stamps lang="en" on the document. The manager has no lookup or negotiation API: #2302 deleted the unused format, bundle, negotiate and with_default_locale until a page consumes them (the maintainer’s delete-until-wired ruling). The en/ and es/ main.ftl files are short portal-* stubs seeded for the day the components pull strings through the manager. Localizing the rendered pages is the remaining i18n work: adding the lookup and locale negotiation, threading a negotiated locale into each component, and replacing inline English with lookups. the Spanish catalog follow-up is tracked as #666 (Roadmap).
Divergence from ADR-008. ADR-008 › Internationalization prescribes a richer negotiation ladder (URL path prefix /es/apply → session/localStorage → Accept-Language → en) and says no English is hard-coded in source. As built, no negotiation exists and the pages are English-only. The ADR’s ladder is the target state.
Bundle layout
services/canopy-portal/locales/
en/
main.ftl
es/
main.ftl
Each subdirectory’s name is the locale identifier (parsed as a unic_langid::LanguageIdentifier). Every *.ftl file in a locale subdirectory is merged into one bundle per locale — splitting into main.ftl, errors.ftl, forms.ftl, … is purely organizational.
The directory is PortalConfig::locales_dir (locales_dir: locales in config/canopy-portal/default.yaml, env override CANOPY_PORTAL__LOCALES_DIR), resolved relative to the process working directory — /app/locales in the image (services/canopy-portal/Dockerfile copies the bundles there), the crate directory under dx serve.
Relationship to jurisdiction display strings
Two string sources exist. Fluent owns translatable copy. Jurisdiction-varying facts — agency name, helpline, state name, the SNAP day figures — come from jurisdiction::load_strings over the pack’s jurisdiction.toml at boot (main.rs, #1226/#1273/#1161), and the pages render them from that module (welcome.rs says "Welcome to canopy.", not a state name). The document <title> is one of those facts (#2622): Dioxus.toml carries only the product name, which dx bakes into the built index.html; app.rs hands jurisdiction::portal_title() (the product name and the state name) to the SSR document, which writes it in place of the built title in every response, and the client sets document.title to the same value through web-sys after hydration. A jurisdiction fact a bundle message needs is a Fluent argument, never a literal: portal-welcome-title reads Welcome to { $state } Benefits (Bienvenido a los Beneficios de { $state }), and a page that renders it passes jurisdiction::state_name() as the state argument; no page renders it yet (#2622). The neutrality-lint gate’s copy-identity detector reads every .ftl under services/*/locales, so a bundle that names a state fails the push. The portal-welcome-subtitle stub (Apply for SNAP, TANF, Medicaid, and more.) still lists programs; before a page consumes it, the list must come from the pack’s [jurisdiction].enabled_programs.
Key conventions
-
Kebab-case (
portal-welcome-title, notportal_welcome_titleorportalWelcomeTitle). -
Namespaced under
portal-*so per-component prefixes (portal-application-status-pending,portal-application-status-approved) stay scannable. -
English values land first — every new key goes into
en/main.ftlbefore any translation bundle. -
Variables use Fluent’s
{$name}placement — no manual escaping in the.ftlsource.
Adding a new key
-
Add the English value to
services/canopy-portal/locales/en/main.ftl. -
Add the Spanish value to
services/canopy-portal/locales/es/main.ftl. Keep the bundles in step, so every key has a value in every locale. If you do not speak Spanish, copy the English value and mark the key for translation review in the MR description; the translation lands in a follow-up MR. -
Boot the portal (
cargo xtask dev start, orcargo xtask dev reloadon a running devstack) —LocaleManager::newfails closed at startup on a malformed.ftl(main.rs:expect("failed to load Fluent locale bundles")). The crate’s unit tests also load the committedlocales/once (the #2622 welcome-title test), socargo xtask test --unit -p canopy-portal(the per-crate lane form in Local Development) catches a bundle that fails to parse.
Adding a new locale
-
mkdir services/canopy-portal/locales/<lang>where<lang>is a Unicode Language Identifier (UTS #35) as parsed byunic-langid(e.g.vi,zh-CN). -
Mirror every key from
en/main.ftlinto<lang>/main.ftl. -
No code change —
LocaleManager::newdiscovers every subdirectory at startup. -
Update this page’s "two day-one locales" wording if the addition is permanent.
Translation review
Intended process — not yet enforced by a CODEOWNERS rule or a release-checklist step; the only other statement of it is the header comment in es/main.ftl:
-
The MR carries the English and non-English values together.
-
A reviewer with native fluency reviews the
.ftldiff. -
Approved translations merge; unapproved translations stay in the MR.
A key that was copied from English in step 2 above ships as English until its translation lands. The review step is what keeps an unreviewed Spanish value out of a release.
Runtime behavior
-
LocaleManager::new(bundle_dir)walks the directory, parses every.ftl, builds oneFluentBundleper locale. Fails closed on a missing directory, an unparseable language identifier, a malformed.ftl, or an empty directory. -
loaded_locales()lists the loaded locales alphabetically, anddefault_locale()reportsDEFAULT_LOCALE("en"). Nothing formats a message or negotiates a locale at runtime yet (#2302). Bundle selection, missing-key rendering and the per-request extractor are designed with the first page that consumes the bundles.
Bidi-isolation
FluentBundle::set_use_isolating(false) is called on every bundle. Fluent’s default wraps every interpolated variable in U+2068 / U+2069 bidi-isolation marks, which become mojibake once the rendered string is HTML-escaped downstream. If an RTL locale is added, this default flips back at that time (separate plan).
Tests
services/canopy-portal/src/i18n.rs has 4 unit tests:
-
Malformed
.ftlfailsLocaleManagerconstruction loudly. -
Empty bundle directory fails loudly.
-
The committed bundles carry
portal-welcome-titlein every locale, taking the state as$state(#2622). -
loaded_locales()returns alphabetically sorted.
The first, second and fourth use a tempfile-backed fixture bundle directory, and the third loads the committed locales/. The applicant-facing pages are exercised by tests/e2e/specs/applicant-portal.spec.ts, whose assertions are against the English strings the pages render today — there is no page-level locale-switch e2e coverage, because no page consumes the manager.
References
-
ADR-008 — Applicant portal architecture (Dioxus + Fluent) — the target negotiation ladder
-
services/canopy-portal/src/i18n.rs—LocaleManager(new/loaded_locales/default_locale) -
services/canopy-portal/src/main.rs— thelocale_managerbinding inserve()and the.layer(axum::Extension(locale_manager))mount -
services/canopy-portal/src/config.rs—PortalConfig::locales_dir -
services/canopy-portal/locales/— bundle files (en/main.ftl,es/main.ftl— short stubs today)