UNPKG

react-intlayer

Version:

Easily internationalize i18n your React applications with type-safe multilingual content management.

1 lines 3.36 kB
{"version":3,"file":"useI18n.mjs","names":["dictionaryContent: DeepTransformContent<DictionaryRegistryContent<T>>","current: any"],"sources":["../../../src/client/useI18n.ts"],"sourcesContent":["'use client';\n\nimport { getIntlayer, type ValidDotPathsFor } from '@intlayer/core';\nimport type {\n DeclaredLocales,\n DictionaryKeys,\n DictionaryRegistryContent,\n GetSubPath,\n LocalesValues,\n} from '@intlayer/types';\nimport { useContext, useMemo } from 'react';\nimport type { DeepTransformContent } from '../plugins';\nimport { IntlayerClientContext } from './IntlayerProvider';\n\n/**\n * Hook that provides a translation function `t()` for accessing nested content by key.\n * This hook mimics the pattern found in libraries like i18next, next-intl, and vue-i18n.\n *\n * @param namespace - The dictionary key to scope translations to\n * @param locale - Optional locale override. If not provided, uses the current context locale\n * @returns A translation function `t(key)` that returns the translated content for the given key\n *\n * @example\n * ```tsx\n * const t = useI18n('IndexPage');\n * const title = t('title'); // Returns translated string for 'IndexPage.title'\n * const nestedContent = t('section.subtitle'); // Returns 'IndexPage.section.subtitle'\n * // For attributes like `aria-label`, use `.value` to get the plain string\n * const ariaLabel = t('button.ariaLabel').value; // 'Close modal'\n * ```\n */\nexport const useI18n = <\n T extends DictionaryKeys,\n L extends LocalesValues = DeclaredLocales,\n>(\n namespace: T,\n locale?: L\n) => {\n const { locale: currentLocale } = useContext(IntlayerClientContext);\n const localeTarget = useMemo(\n () => locale ?? currentLocale,\n [currentLocale, locale]\n );\n\n // Get the dictionary content for the namespace\n // @ts-ignore Type instantiation is excessively deep and possibly infinite\n const dictionaryContent: DeepTransformContent<DictionaryRegistryContent<T>> =\n useMemo(\n () => getIntlayer<T, L>(namespace, localeTarget as L),\n [namespace, localeTarget]\n );\n\n // Return the translation function\n // @ts-ignore Type instantiation is excessively deep and possibly infinite\n const t = <P extends ValidDotPathsFor<T>>(\n path: P\n // @ts-ignore Type instantiation is excessively deep and possibly infinite\n ): GetSubPath<DeepTransformContent<DictionaryRegistryContent<T>>, P> => {\n if (!path) {\n // @ts-ignore Type instantiation is excessively deep and possibly infinite\n return dictionaryContent as any;\n }\n\n const pathArray = (path as string).split('.');\n let current: any = dictionaryContent;\n\n for (const key of pathArray) {\n current = current?.[key];\n if (current === undefined) {\n // Return the whole dictionary as fallback if path is not found\n return dictionaryContent as any;\n }\n }\n\n return current;\n };\n\n return t;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AA+BA,MAAa,WAIX,WACA,WACG;CACH,MAAM,EAAE,QAAQ,kBAAkB,WAAW,sBAAsB;CACnE,MAAM,eAAe,cACb,UAAU,eAChB,CAAC,eAAe,OAAO,CACxB;CAID,MAAMA,oBACJ,cACQ,YAAkB,WAAW,aAAkB,EACrD,CAAC,WAAW,aAAa,CAC1B;CAIH,MAAM,KACJ,SAEsE;AACtE,MAAI,CAAC,KAEH,QAAO;EAGT,MAAM,YAAa,KAAgB,MAAM,IAAI;EAC7C,IAAIC,UAAe;AAEnB,OAAK,MAAM,OAAO,WAAW;AAC3B,aAAU,UAAU;AACpB,OAAI,YAAY,OAEd,QAAO;;AAIX,SAAO;;AAGT,QAAO"}