For AI agents: the complete documentation index is available at https://ifhover.github.io/leaf-ui/v/0.6.0/en/llms.txt, the full documentation bundle is available at https://ifhover.github.io/leaf-ui/v/0.6.0/en/llms-full.txt, and this page is available as Markdown at https://ifhover.github.io/leaf-ui/v/0.6.0/en/api/translations.md.
  • English
  • Translation resources

    Add languages with ConfigProvider translations. Use locale to choose the active language and fallback order. Runnable example.

    Start with JSON

    Copy the complete English template, keeping its keys. It is the same resource used by the components. Read the installed version from the package:

    import enUS from '@ifhover/leaf-ui/locales/en-US.json';

    Built-in zh-CN.json, zh-TW.json, ja-JP.json and ko-KR.json use the same path. Set locale to use a built-in language without importing these files. Resources contain text grouped by component, with shared actions in common. No registration or helper function is required.

    This example uses playful “Martian” text with the private-use tag zh-CN-x-mars. Dates and numbers retain Simplified Chinese regional formatting:

    import { ConfigProvider } from '@ifhover/leaf-ui';
    import mars from './locales/zh-CN-x-mars.json';
    
    const translations = { 'zh-CN-x-mars': mars };
    
    <ConfigProvider locale={['zh-CN-x-mars', 'en-US']} translations={translations}>
      <App />
    </ConfigProvider>;

    Fallback and inheritance

    CaseBehavior
    locale="zh-CN-x-mars"Equivalent to ['zh-CN-x-mars']; missing entries use built-in English
    locale={['zh-CN-x-mars', 'zh-CN']}Look up each entry in Martian text, Simplified Chinese, then built-in English
    First tagControls lang and regional Intl date and number formats, regardless of individual fallback entries
    Built-in language overridesA resource with the same tag overrides only the supplied entries
    Nested translationsMerge by language, group and entry, with inner values taking precedence
    Nested localeOmit to inherit; an explicit value replaces the whole chain
    Empty stringKept as empty; only missing or undefined entries fall back
    Weekday arraysReplace all 7 entries, Monday through Sunday; weekStartsOn controls display order

    Use valid BCP 47 tags. Empty chains and invalid tags throw an error. Specify fr-CA → fr explicitly when desired. The final fallback always uses built-in English. Include en-US in the chain if your English overrides should participate.

    Component props and components text take precedence. Translate application content separately. Resources do not change direction or weekStartsOn; configure those independently.

    Variable sentences

    Sentences use ICU MessageFormat so translators can control word order and plurals:

    {
      "pagination": {
        "total": "{total, plural, one {# item} other {# items}}"
      }
    }

    Keep the variable names from the English template, such as {total} and {label}. Plurals use the language supplying the entry. Malformed templates fall back to built-in English and log the entry in development. Output is rendered as plain text.

    Validate translations

    The package provides a JSON Schema for editor validation. You may add $schema at the resource root pointing to the installed dist/locales/schema.json; runtime translation ignores it.

    Check unknown keys, missing entries and ICU arguments:

    pnpm exec leaf-ui-check-translations ./locales/zh-CN-x-mars.json --allow-partial

    Use --allow-partial for intentionally incomplete resources. Structure, spelling and variables are still checked; missing entries are listed without failing. Run the check again after upgrading to find new entries.

    Types

    import type { LeafTranslation, LeafTranslations } from '@ifhover/leaf-ui';
    
    const mars = {
      select: { placeholder: '挑①個唄' },
    } satisfies LeafTranslation;
    
    const translations = { 'zh-CN-x-mars': mars } satisfies LeafTranslations;

    LeafTranslations

    PropertyTypeDescription
    [languageTag]LeafTranslationPartial or complete resources, indexed by a canonicalized BCP 47 tag

    LeafTranslation

    PropertyTypeDescription
    [group]Partial<Record<entry, string | readonly string[]>>Groups and entries from the English template, such as common.confirm and select.placeholder; completion only allows existing keys
    $schemastringOptional editor schema location, ignored at runtime

    Treat resources as immutable and pass a new object when updating them. Resources stay within the Provider rather than a global registry. For SSR, use the same chain, JSON and initial values on server and client. See SSR.