{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "shared-localize",
  "type": "registry:lib",
  "title": "shared/localize",
  "description": "Internal module shared by the ui-* components (localize). Copied from @purpurabog/ui 2.3.0; the package is the maintained source, this copy is yours to change. Icons from the Carbon Design System (Apache-2.0, © IBM Corp.; see THIRD-PARTY-NOTICES.md in the package).",
  "dependencies": [
    "lit@^3.3.0"
  ],
  "registryDependencies": [
    "@kanok-ui/core-i18n",
    "@kanok-ui/tokens"
  ],
  "files": [
    {
      "path": "src/primitives/shared/localize.ts",
      "type": "registry:lib",
      "target": "components/ui/primitives/shared/localize.ts",
      "content": "import type { ReactiveController, ReactiveControllerHost } from 'lit';\nimport { getTranslation, onTranslationsChange, type UiTranslation } from '../../core/i18n.ts';\n\nexport * from '../../core/i18n.ts';\n\n/** Connected components, re-rendered when the page language or the registry changes. */\nconst hosts = new Set<ReactiveControllerHost & HTMLElement>();\nonTranslationsChange(() => {\n    for (const host of hosts) host.requestUpdate();\n});\n\n/**\n * `lang` / `dir` changes re-render every connected component: anywhere in the document (an ancestor, `<html>`) or in a\n * shadow root that holds components (an ancestor there). One observer per such tree, shared by its components.\n */\nconst treeObservers = new Map<Node, { observer: MutationObserver; users: number }>();\n\nfunction observeTree(root: Node) {\n    if (typeof MutationObserver === 'undefined') return;\n    const known = treeObservers.get(root);\n    if (known) {\n        known.users++;\n        return;\n    }\n    const observer = new MutationObserver(() => {\n        for (const host of hosts) host.requestUpdate();\n    });\n    observer.observe(root, { attributes: true, attributeFilter: ['lang', 'dir'], subtree: true });\n    treeObservers.set(root, { observer, users: 1 });\n}\n\nfunction unobserveTree(root: Node) {\n    const known = treeObservers.get(root);\n    if (!known || --known.users > 0) return;\n    known.observer.disconnect();\n    treeObservers.delete(root);\n}\n\n/**\n * Gives a component its language and text direction: its own `lang` / `dir` attribute, else the\n * closest ancestor's in the same tree, else `<html>`, else the browser's. Re-renders the host when\n * any of those change or a translation is registered.\n */\nexport class LocalizeController implements ReactiveController {\n    private readonly host: ReactiveControllerHost & HTMLElement;\n    /** The trees watched for this host: the document, and its shadow root when it sits in one. */\n    private trees: Node[] = [];\n\n    constructor(host: ReactiveControllerHost & HTMLElement) {\n        this.host = host;\n        host.addController(this);\n    }\n\n    hostConnected() {\n        hosts.add(this.host);\n        const root = typeof this.host.getRootNode === 'function' ? this.host.getRootNode() : null;\n        this.trees = [globalThis.document, root].filter((tree, i, all): tree is Node => !!tree && all.indexOf(tree) === i);\n        for (const tree of this.trees) observeTree(tree);\n    }\n\n    hostDisconnected() {\n        hosts.delete(this.host);\n        for (const tree of this.trees) unobserveTree(tree);\n        this.trees = [];\n    }\n\n    /**\n     * The nearest `name` attribute: the host's, an ancestor's, then `<html>`'s. On the server (Lit SSR)\n     * there are no ancestors or document to ask, so only the host's own attribute counts there.\n     */\n    private inherited(name: 'lang' | 'dir'): string {\n        if (typeof this.host.closest !== 'function') return this.host.getAttribute(name) ?? '';\n        return this.host.closest(`[${name}]`)?.getAttribute(name) || globalThis.document?.documentElement.getAttribute(name) || '';\n    }\n\n    /** Resolved BCP 47 language, e.g. `th-TH`. */\n    lang(): string {\n        return this.inherited('lang') || globalThis.navigator?.language || 'en';\n    }\n\n    /** Resolved text direction. */\n    dir(): 'ltr' | 'rtl' {\n        const explicit = this.inherited('dir');\n        if (explicit === 'rtl' || explicit === 'ltr') return explicit;\n        return this.t.$dir;\n    }\n\n    /** The translation for `lang()`. */\n    get t(): UiTranslation {\n        return getTranslation(this.lang());\n    }\n}\n"
    }
  ]
}
