UNPKG

@lobehub/ui

Version:

Lobe UI is an open-source UI component library for building AIGC web apps

1 lines 1.02 kB
{"version":3,"file":"useTranslation.mjs","names":[],"sources":["../../src/i18n/useTranslation.ts"],"sourcesContent":["import { useMemo } from 'react';\n\nimport { useI18n } from './context';\nimport type { TranslationKey, TranslationResources } from './types';\n\nexport const useTranslation = (fallbackResources?: TranslationResources) => {\n const { t, locale } = useI18n();\n\n const translate = useMemo((): ((key: TranslationKey) => string) => {\n if (!fallbackResources) return t;\n\n return (key: TranslationKey): string => {\n const value = t(key);\n const fallback = fallbackResources[key];\n return value === key && fallback ? fallback : value;\n };\n }, [t, fallbackResources]);\n\n return { locale, t: translate };\n};\n"],"mappings":";;;;AAKA,MAAa,kBAAkB,sBAA6C;CAC1E,MAAM,EAAE,GAAG,WAAW,SAAS;AAY/B,QAAO;EAAE;EAAQ,GAVC,cAAiD;AACjE,OAAI,CAAC,kBAAmB,QAAO;AAE/B,WAAQ,QAAgC;IACtC,MAAM,QAAQ,EAAE,IAAI;IACpB,MAAM,WAAW,kBAAkB;AACnC,WAAO,UAAU,OAAO,WAAW,WAAW;;KAE/C,CAAC,GAAG,kBAAkB,CAAC;EAEK"}