Skip to content

refactor(i18n): install use-intl and establish language registry/provider foundation #373

Closed

Description

@khaira777

Refs: #372
Depends on: None

Context

FloCafe currently uses a hand-rolled translation helper in frontend/src/lib/i18n.ts with 4 static JSON imports, synchronous t(key, lang, params), and no compile-time type checking.

This issue establishes the use-intl v4 foundation, centralizes language metadata into a single registry, sets up the I18nProvider, and enables TypeScript AppConfig module augmentation based on the official use-intl v4 specification.

No components are migrated in this issue. All existing consumers continue working through a backward-compatible adapter in frontend/src/lib/i18n.ts. Lazy loading is deliberately reserved for #375; this issue provides a synchronous compatibility baseline.

Architectural Decisions Relevant to This Issue

  • Library: use-intl v4 (React 19 & Next.js static export compatible).
  • Central Language Registry with Inferred Types: Define LANGUAGES using as const satisfies Record<string, LanguageConfig>. Derive Language as keyof typeof LANGUAGES and Locale from (typeof LANGUAGES)[Language]['locale'] rather than maintaining duplicate union types.
  • use-intl v4 AppConfig Augmentation: Augment use-intl's AppConfig interface in messages.d.ts (not legacy global IntlMessages).
  • Synchronous Compatibility Baseline: In this phase, the provider and compatibility adapter maintain synchronous access to bundled messages so existing views operate without asynchronous bootstrap flicker. Lazy loading is introduced in perf(i18n): lazy load active locale translation bundle #375.
  • Persian (feat(i18n): add Persian/Farsi and Iran support #241) Status: Configured with direction: 'rtl' and selectable: false to maintain technical readiness without bypassing feat(i18n): add Persian/Farsi and Iran support #241 user-facing enablement.

What to do

1. Install use-intl

Install use-intl in the frontend:

cd frontend && npm install use-intl

2. Create Centralized Language Registry

Create frontend/src/lib/i18n/languages.ts:

import en from './messages/en.json';
import es from './messages/es.json';
import pt from './messages/pt.json';
import fa from './messages/fa.json';

export type LanguageDirection = 'ltr' | 'rtl';

export interface LanguageConfig {
  readonly locale: string;
  readonly nativeName: string;
  readonly direction: LanguageDirection;
  readonly selectable: boolean;
  readonly messages: Record<string, unknown>;
  readonly load?: () => Promise<{ default: Record<string, unknown> }>;
}

export const LANGUAGES = {
  en: {
    locale: 'en',
    nativeName: 'English',
    direction: 'ltr',
    selectable: true,
    messages: en,
    load: () => import('./messages/en.json'),
  },
  es: {
    locale: 'es',
    nativeName: 'Español',
    direction: 'ltr',
    selectable: true,
    messages: es,
    load: () => import('./messages/es.json'),
  },
  pt: {
    locale: 'pt-BR',
    nativeName: 'Português',
    direction: 'ltr',
    selectable: true,
    messages: pt,
    load: () => import('./messages/pt.json'),
  },
  fa: {
    locale: 'fa-IR',
    nativeName: 'فارسی',
    direction: 'rtl',
    selectable: false, // Governed by #241 acceptance
    messages: fa,
    load: () => import('./messages/fa.json'),
  },
} as const satisfies Record<string, LanguageConfig>;

export type Language = keyof typeof LANGUAGES;
export type Locale = (typeof LANGUAGES)[Language]['locale'];

export function getLanguageDirection(lang: Language): LanguageDirection {
  return LANGUAGES[lang]?.direction ?? 'ltr';
}

export function getLanguageLocale(lang: Language): string {
  return LANGUAGES[lang]?.locale ?? 'en';
}

3. Move Message Files

Move frontend/src/lib/i18n/{en,es,pt,fa}.json to frontend/src/lib/i18n/messages/{en,es,pt,fa}.json.

4. Create I18nProvider

Create frontend/src/components/providers/I18nProvider.tsx:

'use client';

import { type ReactNode } from 'react';
import { IntlProvider } from 'use-intl';
import { usePosSettingsStore } from '@/store/pos-settings';
import { LANGUAGES, type Language } from '@/lib/i18n/languages';

export function I18nProvider({ children }: { children: ReactNode }) {
  const language = usePosSettingsStore((s) => s.language) as Language;
  const config = LANGUAGES[language] ?? LANGUAGES.en;

  return (
    <IntlProvider locale={config.locale} messages={config.messages}>
      {children}
    </IntlProvider>
  );
}

Mount I18nProvider in frontend/src/app/layout.tsx wrapping the root content and client layout components.

5. Establish use-intl v4 AppConfig Augmentation

Create frontend/src/lib/i18n/messages.d.ts using en.json and registered BCP-47 locales:

import type en from './messages/en.json';
import type { Locale } from './languages';

type Messages = typeof en;

declare module 'use-intl' {
  interface AppConfig {
    Messages: Messages;
    Locale: Locale;
  }
}

6. Maintain Backward-Compatible Bridge in i18n.ts

Update frontend/src/lib/i18n.ts to import Language, LanguageDirection, and direction helpers from ./i18n/languages. Re-export them so existing callers (HtmlLangSync, DirectionalToaster, useI18n, etc.) remain fully functional.

What NOT to do

Acceptance Criteria

  • use-intl v4 installed in frontend/package.json.
  • Central language registry created at frontend/src/lib/i18n/languages.ts with as const satisfies typing and derived Language / Locale types.
  • Translation JSON files relocated to frontend/src/lib/i18n/messages/.
  • I18nProvider created and mounted in frontend/src/app/layout.tsx.
  • Official use-intl AppConfig module augmentation active in frontend/src/lib/i18n/messages.d.ts.
  • Full backward compatibility maintained for existing useI18n() and t() callers.
  • No blank screen or asynchronous loading artifacts introduced.

Verification

npm run lint
npm run build:frontend
npm test
npm run test:translations

Activity

  1. added
    enhancement
    area:frontend
    on Aug 17, 2026
  2. changed the title [-]refactor(i18n): install use-intl, add language registry and IntlProvider[/-] [+]refactor(i18n): install use-intl and establish language registry/provider foundation[/+] on Aug 18, 2026
  3. khaira777 commented on Aug 19, 2026

    @khaira777
    ContributorAuthor

    Implemented in PR #395: #395

    What landed:

    • Installed use-intl v4 (^4.13.7) in the frontend.
    • Added the centralized language registry (frontend/src/lib/i18n/languages.ts) using as const satisfies Record<string, LanguageConfig> with derived Language / Locale types.
    • Relocated the four translation JSON files to frontend/src/lib/i18n/messages/.
    • Added the use-intl v4 AppConfig module augmentation (messages.d.ts) — confirmed active, not silently falling back to Record<string, any>.
    • Created and mounted I18nProvider in the root layout.
    • Preserved every existing t() / useI18n() consumer via backward-compatible re-exports in i18n.ts.
    • Updated tests/translations.test.ts and tests/e2e-argentina-flow.test.ts for the new messages/ path.

    Verification: frontend build (static export + full TypeScript check) ✅, frontend lint ✅, npm run test:translations ✅, and the no-mistakes pipeline passed with zero findings (CI green).

    Leaving this issue open for review/merge. Next dependency in the epic is #374 (flat → nested message conversion).

    Noted (out of scope for #373, left for later phases):

    • getBrowserLanguage() still uses string-prefix matching; the epic wants BCP-47 Intl.Locale matching.
    • HtmlLangSync / KdsHtmlLang still hard-code locale tags.
  4. self-assigned this
    on Aug 19, 2026
  5. khaira777 commented on Aug 19, 2026

    @khaira777
    ContributorAuthor

    ✅ Done — merged as 9e32613 via PR #395

    Foundation complete:

    • use-intl@^4.13.7 installed in frontend/.
    • frontend/src/lib/i18n/languages.ts — central language registry (as const satisfies Record<string, LanguageConfig>; derived Language/Locale; Persian selectable:false).
    • frontend/src/lib/i18n/messages.d.ts — use-intl v4 AppConfig augmentation (verified live).
    • frontend/src/components/providers/I18nProvider.tsx mounted in layout.tsx.
    • 4 JSON files moved to frontend/src/lib/i18n/messages/; i18n.ts re-exports registry as backward-compatible bridge.
    • Tests updated (translations.test.ts, e2e-argentina-flow.test.ts).

    Deferred to later phases (recorded here so they aren't lost):

    1. getBrowserLanguage() still uses string-prefix matching; epic wants BCP-47 Intl.Locale matching.
    2. HtmlLangSync / KdsHtmlLang hard-code locale tags (fa-IR/es/pt-BR/en); should use getLanguageLocale().

    Closing.

  6. khaira777 commented on Aug 19, 2026

    @khaira777
    ContributorAuthor

    Closed as completed.

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

Metadata

Metadata

Assignees

Labels

area:frontendenhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions