# XWUILocale

Lightweight i18n controller. Fetches per-locale JSON, swaps `<html lang>` + `<html dir>` on activation, persists the chosen locale, and exposes a static `t()` helper. One instance per page is enough.

## Basic Usage

Mount + use `XWUILocale.t()` for translations.

```example
file: examples/BasicUsage.ts
html: examples/BasicUsage.html
title: Basic Usage
description: Mount + use `XWUILocale.t()` for translations.
```

## Init With Scopes

Static `XWUILocale.init({...})` with multiple string scopes (common / crm / etc.).

```example
file: examples/InitWithScopes.ts
html: examples/InitWithScopes.html
title: Init With Scopes
description: Static `XWUILocale.init({...})` with multiple string scopes (common / crm / etc.).
```

## Preloaded RTL

Inline English + Arabic files with a runtime switch that flips `<html dir>` to rtl.

```example
file: examples/PreloadedRtl.ts
html: examples/PreloadedRtl.html
title: Preloaded RTL
description: Preloaded locales, RTL direction flip and {{name}} interpolation.
```

## Plurals And Switch

Runtime-registered locales with a language toggle and count-driven pluralization.

```example
file: examples/PluralsAndSwitch.ts
html: examples/PluralsAndSwitch.html
title: Plurals And Switch
description: registerLocale + onChange + CLDR plural forms via t({ count }).
```

## Missing-key machine translation (opt-in)

Offline JSON packs remain the source of truth. When a key is missing from the
active locale **and** the fallback locale, hosts may install an async provider.
`t()` still returns the raw key immediately; the fill merges in the background
and fires `xwui-locale-changed` so reactive UI refreshes.

```ts
import {
  XWUILocale,
  createFreeMissingKeyProvider,
} from '@exonware/xwui';

await XWUILocale.init({
  basePath: '/locales',
  fallbackLocale: 'en',
  missingKeyProvider: createFreeMissingKeyProvider(), // MyMemory → LibreTranslate
  missingKeyCache: { persist: true, concurrency: 2 },
});
```

Prefer **build-time** fill of locale JSON for production. Public free endpoints
have quotas/CORS/ToS limits; self-hosted LibreTranslate is the production path.
Unofficial Google scrape endpoints are not supported.

### Performance / WASM

- MT fill + provider response caches use **FastLru** (xwnode algorithm via
  `createStringLru`) - bounded, O(1), no multi‑MB download.
- `localeAccel: true` enables opportunistic **xwsystem** decode for msgpack/cbor
  locale payloads when that port is already warm.
- `localeAccel: { initWasm: true }` eagerly loads system+node - only for apps
  that already want wasm. Plain JSON packs stay on native `res.json()` (faster
  than xwjson for kilobyte files; xwjson is not vendored in xwui dist).

```api
```
