Skip to content

An open source design system that's fully customizable and agent ready

TypeScript
5.6 KB
Raw Blame
// Copyright (c) Meta Platforms, Inc. and affiliates.

'use client';

/**
 * @file InternationalizationProvider.tsx
 * @input React, InternationalizationContext, i18n types, calendar resolution
 * @output Exports InternationalizationProvider component and props type
 * @position Provider component for astryx i18n locale, messages, and calendar
 *
 * Wraps a subtree with a locale and (optional) additional message catalogs +
 * overrides. Astryx components inside the subtree resolve their strings via
 * this context. If a consumer never renders a provider, astryx components
 * still work — they use the shipped en catalog directly.
 *
 * SYNC: When modified, update these files to stay in sync:
 * - /packages/core/src/i18n/InternationalizationContext.ts
 * - /packages/core/src/i18n/index.ts
 * - /packages/core/src/i18n/InternationalizationProvider.doc.mjs
 */

import {useMemo, type ReactNode} from 'react';
import {InternationalizationContext} from './InternationalizationContext';
import {getLocaleDirection} from './getLocaleDirection';
import {resolveCalendarSystem} from './useCalendarSystem';
import type {CalendarSystem} from '../utils/calendarSystem';
import type {
  Locale,
  MessagesByLocale,
  Overrides,
  ProviderMessagesByLocale,
} from './types';
import {getResolve} from './resolve';

const PROVIDER_NAME = 'InternationalizationProvider';

function normalizeMessages(
  messages: ProviderMessagesByLocale,
): MessagesByLocale {
  const hasRuntimeCatalog = Object.values(messages).some(catalog =>
    Object.values(catalog).some(entry => typeof entry === 'string'),
  );
  if (!hasRuntimeCatalog) {
    return messages as MessagesByLocale;
  }

  return Object.fromEntries(
    Object.entries(messages).map(([locale, catalog]) => [
      locale,
      Object.fromEntries(
        Object.entries(catalog).map(([key, entry]) => [
          key,
          typeof entry === 'string' ? {defaultMessage: entry} : entry,
        ]),
      ),
    ]),
  );
}

export interface InternationalizationProviderProps {
  /**
   * BCP 47 language tag. Examples: `'en'`, `'pt'`, `'pt-BR'`, `'zh-Hans'`.
   *
   * Regional tags are respected — resolving a message walks the tag from
   * most-specific to least-specific (`pt-BR` → `pt`), then falls back to
   * the shipped `en` catalog.
   */
  locale: Locale;
  /**
   * Additional shipped catalogs to make available for the selected locale.
   * `en` is bundled with astryx and never needs to be listed here.
   *
   * @example
   * ```
   * import frFR from '@astryxdesign/core/locales/fr-FR.generated.js';
   * <InternationalizationProvider
   *   locale="fr-FR"
   *   messages={{'fr-FR': frFR}}>
   * ```
   */
  messages?: ProviderMessagesByLocale;
  /**
   * Sparse per-locale overrides applied on top of shipped defaults.
   * Only the keys you want to override need to be listed.
   *
   * @example
   * ```
   * <InternationalizationProvider
   *   locale="fr"
   *   overrides={{fr: {'@astryx.pagination.next': 'Suivant'}}}>
   * ```
   */
  overrides?: Overrides;
  /**
   * Optional explicit text direction override. When omitted, direction is derived
   * from `locale` via `Intl.Locale.getTextInfo()`. Provide this to force a
   * direction (e.g. RTL layout testing under an English catalog) or to skip the
   * derivation when you already know the direction.
   *
   * This sets the direction astryx reads via context; it does NOT set the DOM
   * `dir` attribute. You must set `dir` on `<html>` (or a wrapping element)
   * yourself — astryx components mirror layout and directional icons from the
   * DOM `dir`, so an RTL locale won't visually mirror without it. Keep the two
   * in sync (e.g. `dir={getLocaleDirection(locale)}` on both).
   */
  dir?: 'ltr' | 'rtl';
  /**
   * The calendar system of this subtree, such as `'persian'` or `'hebrew'`.
   * `Calendar` shows it, and Timestamp, PowerSearch, and Lab's Schedule write
   * their dates in it. `DateInput`, `DateRangeInput`, and `DateTimeInput`
   * show it too. Values stay
   * Gregorian ISO dates. When omitted, the calendar is Gregorian: a locale
   * never selects it, neither by its default calendar nor by a `-u-ca-`
   * keyword. A component's own `calendar` prop takes precedence. Calendars
   * other than `gregory`, `iso8601`, `buddhist`, `roc`, and `japanese` need
   * `Temporal`. The context keeps the requested calendar; where `Temporal` is
   * missing, these components show Gregorian dates and warn once in
   * development.
   *
   * @example
   * ```
   * <InternationalizationProvider locale="fa-IR" calendar="persian">
   * ```
   */
  calendar?: CalendarSystem;
  children: ReactNode;
}

/**
 * Provides locale + additional messages + overrides to all astryx
 * components in the subtree.
 */
export function InternationalizationProvider({
  locale,
  messages,
  overrides,
  dir,
  calendar: calendarProp,
  children,
}: InternationalizationProviderProps) {
  const direction = dir ?? getLocaleDirection(locale);
  const calendar = useMemo(
    () =>
      calendarProp != null
        ? resolveCalendarSystem(calendarProp, PROVIDER_NAME)
        : 'gregory',
    [calendarProp],
  );
  const value = useMemo(() => {
    const providedMessages = messages ?? {};
    return {
      locale,
      direction,
      calendar,
      messages: normalizeMessages(providedMessages),
      overrides,
      translate: getResolve(locale, providedMessages, overrides),
    };
  }, [locale, direction, calendar, messages, overrides]);
  return (
    <InternationalizationContext value={value}>
      {children}
    </InternationalizationContext>
  );
}

InternationalizationProvider.displayName = 'InternationalizationProvider';