{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "i18n-context",
  "title": "I18n context",
  "description": "I18nProvider and useI18n: the current locale, the available locales and the UI string translations, which RootProvider's i18n prop sets.",
  "dependencies": [
    "@fuma-translate/react@^1.0.2",
    "fumadocs-core@^16.17.8"
  ],
  "files": [
    {
      "path": "registry/base/docs/contexts/i18n.tsx",
      "content": "// Adapted from Fumadocs UI (https://github.com/fuma-nama/fumadocs)\n// Copyright (c) 2023 Fuma, MIT License\n\"use client\";\nimport {\n  createContext,\n  type ReactNode,\n  use,\n  useLayoutEffect,\n  useMemo,\n  useRef,\n} from \"react\";\nimport { usePathname, useRouter } from \"fumadocs-core/framework\";\nimport type { I18nConfig } from \"fumadocs-core/i18n\";\nimport { TranslationProvider } from \"@fuma-translate/react\";\n\ninterface LocaleItem {\n  name: string;\n  locale: string;\n}\n\ninterface LocaleContextType {\n  locale?: string;\n  onChange?: (v: string) => void;\n  locales?: LocaleItem[];\n}\n\nconst LocaleContext = createContext<LocaleContextType>({});\n\nexport function useI18n(): LocaleContextType {\n  return use(LocaleContext);\n}\n\nexport interface I18nProviderProps extends Partial<\n  Pick<I18nConfig, \"defaultLanguage\" | \"hideLocale\">\n> {\n  locale?: string;\n  onLocaleChange?: (v: string) => void;\n  translations?: Partial<Record<string, string>>;\n  locales?: LocaleItem[];\n  children?: ReactNode;\n}\n\nconst Empty = {};\n\nexport function I18nProvider({\n  locales = [],\n  locale,\n  defaultLanguage,\n  hideLocale = \"never\",\n  onLocaleChange,\n  children,\n  translations = Empty,\n}: I18nProviderProps) {\n  const router = useRouter();\n  const pathname = usePathname();\n  const onChange = (value: string) => {\n    if (onLocaleChange) {\n      return onLocaleChange(value);\n    }\n    let path = pathname;\n    if (\n      hideLocale === \"never\" ||\n      (hideLocale === \"default-locale\" && locale !== defaultLanguage)\n    ) {\n      const end = pathname.indexOf(\"/\", 1);\n      path = end === -1 ? \"/\" : pathname.slice(end);\n    }\n\n    // With `always`, middleware uses the prefix to update the locale cookie before redirecting.\n    if (hideLocale !== \"default-locale\" || value !== defaultLanguage) {\n      path = `/${value}${path === \"/\" ? \"\" : path}`;\n    }\n\n    router.push(path);\n  };\n  const onChangeRef = useRef(onChange);\n  useLayoutEffect(() => {\n    onChangeRef.current = onChange;\n  });\n\n  return (\n    <LocaleContext\n      value={useMemo(\n        () => ({\n          locale,\n          locales,\n          onChange: (v) => onChangeRef.current(v),\n        }),\n        [locale, locales],\n      )}\n    >\n      <TranslationProvider translations={translations}>\n        {children}\n      </TranslationProvider>\n    </LocaleContext>\n  );\n}\n",
      "type": "registry:component",
      "target": "@components/docs/contexts/i18n.tsx"
    }
  ],
  "type": "registry:component"
}