@molecule/app-i18n-default-react

Feature · i18n-default · App (browser) · v1.1.1 · Apache-2.0

Renamed to @molecule/app-i18n-default, which it re-exports unchanged; import from @molecule/app-i18n-default instead.

npm install @molecule/app-i18n-default-react

npm · Source on GitHub

How it works

@molecule/app-i18n-default-react is a ready-made i18n-default feature for the app (browser) side. It composes the core interfaces it needs, so it works with whichever providers your app has bonded.

// Before
import { setupI18nDefault } from '@molecule/app-i18n-default-react'
// After
import { setupI18nDefault } from '@molecule/app-i18n-default'

Works with: @molecule/app-i18n, @molecule/app-locales-common, @molecule/app-storage

Reference

@molecule/app-i18n-default-react — renamed to @molecule/app-i18n-default.

The default i18n setup never used React. @molecule/app-i18n-default is the same code under a framework-neutral name, and React, Vue, Svelte, Solid and Angular apps all use it. This package re-exports it unchanged, so an app that installs this name keeps working.

Auto-generated, AI-first package reference for the molecule.dev ecosystem. It is written to be read by coding agents as much as by people, and is generated from this package's source — edit src/index.ts JSDoc, not this file.

Quick Start

// Before
import { setupI18nDefault } from '@molecule/app-i18n-default-react'
// After
import { setupI18nDefault } from '@molecule/app-i18n-default'

Type

feature

Installation

npm install @molecule/app-i18n-default-react @molecule/app-i18n @molecule/app-i18n-default @molecule/app-locales-common @molecule/app-storage

API

Interfaces

LanguageDefinition

Static metadata for the 80 languages supported by the molecule flagship fleet. Each entry includes the locale code, native name, and text direction.

Lifted from the byte-identical per-app bonds/i18n-default.ts files that every fleet app used to ship.

interface LanguageDefinition {
  code: string
  name: string
  direction: 'ltr' | 'rtl'
}

Types

PackageLocaleLoader

One companion bond's lazy per-language loader — the loadLocaleModule export of @molecule/app-locales-<name>/loader.js.

type PackageLocaleLoader = (code: string) => Promise<Record<string, string> | undefined>
  • code — Locale code, e.g. en, pt, zh-TW.

Returns: That bond's translations for the locale, or undefined when the bond has none.

PackageLocaleBond

Shape of a per-package locale bond passed via packageLocales.

Locale bond packages (@molecule/app-locales-<name>) follow a uniform layout: each <lang>.ts file exports a constant matching the locale code, and the package's src/index.ts re-exports all of them as a barrel. Consumers do import * as bond from '@molecule/app-locales-<name>' to get an object keyed by locale code (with types and a few non-locale exports also present).

This type captures the "anything except the types module is a {lang: translations} map" shape via Record<string, unknown> with a per-key runtime filter at registration time.

type PackageLocaleBond = Record<string, unknown>

SetupI18nDefaultOptions

Options accepted by setupI18nDefault.

Import the primary UI dictionary eagerly and pass it as primaryUi with its primaryLocale code. Existing English-first callers can pass enUi.

type SetupI18nDefaultOptions = (
  | { enUi: Record<string, string>; primaryLocale?: never; primaryUi?: never }
  | { enUi?: never; primaryLocale: string; primaryUi: Record<string, string> }
) & {
  /**
   * Per-locale lazy loader for the app's `ui.ts`. The callback runs
   * on demand when the i18n provider switches to that locale; merging
   * with the universal common-locale bond is handled inside this
   * package, so callers only need to return the app-specific UI keys.
   */
  lazyLoadUi: (code: string) => Promise<Record<string, string>>
  /**
   * Per-package locale bonds to eagerly register with the i18n provider.
   *
   * Each entry is a star-imported `@molecule/app-locales-<name>` module.
   * Every property that LOOKS like a locale (matches `^[a-z]{2,3}(-[A-Z]{2,4})?$`
   * and resolves to a non-empty object of string values) is registered
   * via `provider.addTranslations(lang, translations)` at setup time.
   *
   * @example
   * ```ts
   * import * as authLocales from '@molecule/app-locales-auth'
   * import * as legalLocales from '@molecule/app-locales-legal-default'
   *
   * setupI18nDefault({
   *   enUi: en,
   *   lazyLoadUi,
   *   packageLocales: [authLocales, legalLocales],
   * })
   * ```
   *
   * Per-app `ui.ts` (registered via `lazyLoadUi`) still overrides
   * package-locale translations because it's merged in second.
   */
  packageLocales?: readonly PackageLocaleBond[]
  /**
   * Per-package locale bonds registered LAZILY, one language at a time.
   *
   * Each entry is that bond's `loadLocaleModule` export, imported from
   * `@molecule/app-locales-<name>/loader.js`. Nothing from these bonds is in
   * the entry bundle: the loader is called with a locale code the first time
   * that locale is selected, and only that language's dictionary is fetched.
   *
   * Prefer this over `packageLocales`: a star-import of the bond pulls
   * every one of its ~79 dictionaries into the importing chunk (several
   * megabytes across a typical app's dependencies).
   *
   * @example
   * ```ts
   * import { loadLocaleModule as authLocales } from '@molecule/app-locales-auth/loader.js'
   *
   * setupI18nDefault({ enUi: en, lazyLoadUi, packageLocaleLoaders: [authLocales] })
   * ```
   */
  packageLocaleLoaders?: readonly PackageLocaleLoader[]
  /**
   * Optional allowlist of locale codes the app actually supports.
   *
   * When provided, only these locales are registered with the i18n
   * provider — so `useTranslation().locales` (and therefore any
   * language-picker UI built on top of it) renders ONLY supported
   * choices. The primary locale is always included.
   *
   * When omitted, every entry in the fleet's 80-language
   * `LANGUAGE_DEFINITIONS` list is registered, then each secondary
   * locale's `ui.ts` is loaded through `lazyLoadUi` at boot and the empty
   * ones are removed: one request per language on a first visit.
   *
   * Read it from the app's locale file names with a NON-eager
   * `import.meta.glob`, which fetches nothing to build the list
   * (`{ eager: true }` would put every language in the entry chunk):
   *
   * ```ts
   * const localeFiles = import.meta.glob<{ ui: Record<string, string> }>('../locales/*\/ui.ts')
   * const supportedLocales = Object.keys(localeFiles).map((file) =>
   *   file.replace('../locales/', '').replace('/ui.ts', ''),
   * )
   * ```
   */
  supportedLocales?: readonly string[]
  /** Optional factory for apps using an alternate i18n bond. */
  createProvider?: (primaryLocale: string, locales: LocaleConfig[]) => I18nProvider
}

Functions

setupI18nDefault(options)

Configure i18n with synchronous English common translations when English is primary. Import from @molecule/app-i18n-default/setup.js for a different primary locale.

function setupI18nDefault(options: SetupI18nDefaultOptions): I18nProvider
  • options — App dictionaries, offered locales, and companion loaders.

Returns: The configured provider.

Constants

LANGUAGE_DEFINITIONS

Canonical 80-language definition list. The names are written in the language's own script for the language picker dropdown.

const LANGUAGE_DEFINITIONS: readonly LanguageDefinition[]

Injection Notes

Requirements

Peer dependencies:

  • @molecule/app-i18n ^1.4.0
  • @molecule/app-locales-common ^1.0.5
  • @molecule/app-storage ^1.0.1

Runtime Dependencies

  • @molecule/app-i18n
  • @molecule/app-i18n-default
  • @molecule/app-locales-common
  • @molecule/app-storage