Skip to content

refactor(i18n): migrate to scalable use-intl architecture #372

Closed
12 of 12 issues completed
Closed

Description

@khaira777

Context

FloCafe's internationalization currently relies on a custom synchronous translation engine (frontend/src/lib/i18n.ts and frontend/src/hooks/useI18n.ts) with flat translation keys, bundled translation maps, regex-based ICU pluralization, and string-prefix locale matching.

This epic refactors FloCafe's frontend internationalization to use use-intl (the same runtime library standard in modern React/Next.js ecosystems) alongside our existing centralized language registry, active-locale lazy loading, atomic direction transitions, and strong repository-native CI validation.

Architectural Principles

  1. Decoupling of i18n, regional settings, and tax compliance: UI translation is strictly decoupled from business logic, tax compliance packs, and store currency/timezone configuration.
  2. Offline-first operation: Translations load locally from static JSON assets bundled with the desktop build. Zero network calls or external SaaS dependencies are introduced.
  3. Runtime library: Migrate to use-intl v4. FloCafe is an Electron / Next.js static-export application and does not need Next.js locale routing, middleware, or server request localization. use-intl provides ICU MessageFormat, useTranslations, createTranslator, type augmentation via AppConfig, and client-side IntlProvider integration.
  4. Centralized language registry: frontend/src/lib/i18n/languages.ts is the single source of truth for supported UI languages, BCP-47 locale tags, native display names, text directions (ltr / rtl), user-facing selectability, and dynamic chunk loaders.
  5. Active locale lazy loading with shared cache: Only the active locale bundle loads during initial startup. Additional locales load on demand via a shared loader/cache (frontend/src/lib/i18n/loader.ts) that deduplicates in-flight requests and caches loaded messages for both React components and non-React consumers.
  6. Atomic UI & direction transition: Active loaded language, messages, <html lang>, <html dir>, and direction-dependent UI elements transition atomically once messages are ready, preventing document direction flipping ahead of message resolution or raw-key flashing.
  7. English canonical schema & strict 100% leaf key parity: frontend/src/lib/i18n/messages/en.json defines the canonical structure (namespaces, keys, ICU variable names). Every committed locale in the repository (including in-progress selectable: false locales) must maintain 100% leaf key parity with en.json to prevent runtime fallback errors.
  8. Nested message structure: Mechanical conversion from flat dotted keys ("auth.signIn") to nested objects ("auth": { "signIn": ... }) required by use-intl.
  9. Strict type safety: Translation keys and locales are verified at compile time via use-intl v4 AppConfig module augmentation. Application domain mappings use exhaustively typed lookup objects (satisfies Record<DomainEnum, MessageKey>) instead of unchecked Record<string, string> maps or runtime string interpolation.
  10. BCP-47 browser language matching: Browser language resolution parses BCP-47 language and regional tags via Intl.Locale against selectable registered languages rather than brittle string prefix matching.
  11. Strong repository-native CI validation: Local and CI checks (npm run test:translations) enforce registry consistency, nested key parity, string leaf validity, ICU parse validation, and parameter/tag placeholder matching before merge.
  12. Phased non-breaking migration: A backward-compatible bridge allows incremental component migration across PR-sized batches without breaking existing views. Only the final cleanup issue removes the legacy bridge.
  13. Language enablement vs. technical availability: A language can be implemented in the registry while remaining hidden from end users (selectable: false). Persian (fa) technical migration is included here, but user-facing enablement remains governed by feat(i18n): add Persian/Farsi and Iran support #241.
  14. No behavioral or wording changes: This epic is strictly an architectural refactoring. No translation copy, country profiles, tax configurations, or product defaults are modified.

Target Contributor Workflow

Adding a new language will follow this standardized workflow:

1. Add frontend/src/lib/i18n/messages/<lang>.json (copy en.json or run npm run i18n:add -- <lang>)
2. Add <lang> entry to frontend/src/lib/i18n/languages.ts
3. Translate leaf string values
4. Run npm run test:translations (or npm run i18n:check)
5. Test UI & RTL if applicable
6. Submit Pull Request

Sequencing

Phase 1 — Foundation & Safety (sequential)
  #373 (Install use-intl & registry foundation)
    ↓
  #374 (Convert flat messages to nested JSON)
    ↓
  #382 (Repository-native validation & CI checks)

Phase 2 — Performance (sequential)
  #375 (Lazy load active locale bundle with shared cache & atomic transitions)

Phase 3 — Component Migration (can run in parallel)
  #376 (Batch 5A: shared, layout, auth, setup)
  #377 (Batch 5B: POS & non-React print helper)
  #378 (Batch 5C: orders, tables, customers, dashboard)
  #379 (Batch 5D: KDS, Server App)
  #380 (Batch 5E: settings, admin, products, remaining)

Phase 4 — Legacy Cleanup (after all of Phase 3)
  #381 (Remove legacy custom i18n engine & hook)

Phase 5 — Contributor Ergonomics & Documentation (sequential)
  #383 (Repo-native contributor & maintenance tooling)
    ↓
  #384 (Git-native contributor documentation)

Sub-issues

Epic Acceptance Criteria

  • All 4 current languages (en, es, pt, fa) render and function identically to before.
  • RTL layout and bidirectional isolation (<Ltr>, .rtl-flip, <html dir>) verified intact across all existing RTL test suites.
  • Only the active locale bundle is loaded during initial application boot.
  • Language switching executes smoothly without raw-key flashing, layout unmounting, or direction desynchronization.
  • Adding a new language requires only one message file and one registry entry in languages.ts.
  • CI validation rejects missing keys, orphan keys, invalid ICU syntax, non-string leaves, and placeholder argument mismatches.
  • All dynamic translation keys are replaced with exhaustively typed lookup mappings.
  • Legacy custom i18n.ts engine and useI18n hook are completely removed after zero consumers remain.
  • Zero external translation network calls/dependencies; desktop app operates fully offline.
  • Contributor guide accurately documents the offline Git-native contribution workflow.

Phase Progress Summary (All 5 Phases Complete)

Sub-issues

Sub-issues

12 of 12 Issues completed
Loading 12 sub-issues

Activity

  1. added
    area:frontend
    epic
    on Aug 17, 2026
  2. changed the title [-]refactor(i18n): migrate to use-intl architecture[/-] [+]refactor(i18n): migrate to scalable use-intl architecture[/+] on Aug 18, 2026
  3. self-assigned this
    on Aug 18, 2026
  4. added theissue type on Aug 18, 2026
  5. khaira777 commented on Aug 19, 2026

    @khaira777
    ContributorAuthor

    Progress update — Phase 1:

  6. khaira777 commented on Aug 19, 2026

    @khaira777
    ContributorAuthor

    Progress update — Phase 1 complete 🎉

    Issue What PR Status
    #373 use-intl install + registry/provider foundation #395 ✅ merged (9e32613)
    #374 flat → nested message conversion #396 ✅ merged (9236866)
    #382 repo-native validation & integrity checks (in flight) 🚧 implemented, no-mistakes pipeline running

    Phase 1 (foundation → nested → validation gate) is now functionally complete. #382's validation suite is the safety gate that protects all subsequent component migration PRs (#375–#384):

    • Registry↔file consistency (BCP-47, direction)
    • en-canonical leaf key parity (incl. selectable: false fa)
    • String leaf validity + ICU syntax validation (@formatjs/icu-messageformat-parser)
    • ICU placeholder/selector + rich-text tag parity vs English
    • TypeScript key safety (exhaustively typed dynamic keys, AppConfig augmentation)
    • fa no-silent-English-fallback safeguards
    • Fixture-based negative tests for every failure mode

    It already caught a real es.json plural bug and a pre-existing 3+ segment key coverage gap. Full detail on #382.

    Phase 2 onwards (ready to kick off once #382 merges): #375 (lazy load) → #376–#380 (component migration, parallel) → #381 (remove legacy engine) → #383 → #384 (tooling/docs).

  7. khaira777 commented on Aug 19, 2026

    @khaira777
    ContributorAuthor

    ✅ Phase 1 complete — all three sub-issues merged & closed

    Issue What PR Status
    #373 use-intl install + registry/provider foundation #395 ✅ merged 9e32613 · closed
    #374 flat → nested message conversion #396 ✅ merged 9236866 · closed
    #382 repo-native validation & integrity checks #397 ✅ merged 588cc8c · closed

    Phase 1 = foundation → nested structure → safety gate, all shipped. The #382 validation suite is now the enforced gate protecting every subsequent migration PR and community translation contribution (registry↔file consistency, en-canonical leaf parity, string leaf validity, ICU syntax, placeholder/selector + rich-text tag parity, TS key safety, fa fallback safeguards, negative fixture tests). It's wired into npm test and exposed as npm run i18n:check.

    Next up (Phase 2):

    Carried-forward notes from #373 (deferred):

    1. getBrowserLanguage() → BCP-47 Intl.Locale matching.
    2. HtmlLangSync / KdsHtmlLang → use getLanguageLocale() instead of hard-coded tags.
  8. khaira777 commented on Aug 19, 2026

    @khaira777
    ContributorAuthor

    ✅ Phase 2 complete — #375 merged & closed

    Issue What PR Status
    #375 lazy-load active locale bundle (shared cache + atomic transitions) #398 ✅ merged 8df666a · closed

    Phase 2 = lazy locale loading, shipped. Only the active locale bundle is fetched at startup — every static-export page now eagerly loads just packaged English (95 KB); es/pt/fa (~357 KB) are on-demand chunks (~79% startup translation payload reduction). I18nProvider resolves the initial language deterministically (store → browser match → English), protects against race conditions from rapid switching, reverts gracefully on failure, and transitions messages + <html lang>/<html dir> atomically. A new build-output test (test:locale-chunks) enforces the lazy-chunk + offline invariants.

    Next up (Phase 3 — component migration, parallelizable):

    Then #381 (remove legacy engine) → #383/#384 (tooling/docs).

    Carried-forward notes (unchanged from #373):

    1. getBrowserLanguage() → BCP-47 Intl.Locale matching.
    2. HtmlLangSync / KdsHtmlLang → use getLanguageLocale() instead of hard-coded tags. (perf(i18n): lazy load active locale translation bundle #375 already moved these to getLanguageLocale() for the atomic lang/dir sync — item 2 is effectively resolved.)
  9. khaira777 commented on Aug 19, 2026

    @khaira777
    ContributorAuthor

    All 12 sub-issues (#373 through #384) and all 5 phases across Epic #372 have landed in main:

    1. Phase 1 (Foundations & Validation): use-intl v4 runtime foundation, nested JSON message format, repository-native validation suite (refactor(i18n): install use-intl and establish language registry/provider foundation #373, refactor(i18n): convert flat translation keys to nested message format #374, chore(i18n): add repository-native translation validation and integrity checks #382).
    2. Phase 2 (Performance & Lazy Loading): Active-locale lazy loading, shared cache, and atomic direction transitions (perf(i18n): lazy load active locale translation bundle #375).
    3. Phase 3 (Component Migration): All components, POS, KDS, Server App, receipts, settings, and tables migrated to useTranslations() and typed enum maps (refactor(i18n): component migration 5A — shared, layout, auth, setup #376, refactor(i18n): component migration 5B — POS #377, refactor(i18n): component migration 5C — orders, tables, customers, dashboard #378, refactor(i18n): component migration 5D — KDS, Server App #379, refactor(i18n): component migration 5E — settings, admin, remaining #380).
    4. Phase 4 (Legacy Engine Cleanup): Custom legacy engine and useI18n hook removed (refactor(i18n): remove legacy custom i18n implementation #381).
    5. Phase 5 (Tooling & Documentation): Offline scaffolding tooling (npm run i18n:add), validation (npm run i18n:check), and comprehensive contributor guide at docs/i18n.md (chore(i18n): add repo-native language contribution and maintenance tooling #383, docs(i18n): contributor guide for adding new languages #384).

    FloCafe internationalization is 100% offline-first, Git-native, and fully modernized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area:frontendepic

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions