import type { UIStrings } from '@/frame/components/context/MainContext' import { useMainContext } from '@/frame/components/context/MainContext' import { createTranslationFunctions } from '@/languages/lib/translation-utils' // Used to pull translation UI strings from the page props into // React components. When you instantiate the hook you can pass // the name or names of the namespaces you want to use. Then, when // referring to specific keys you don't have to say the namespace // (or which of the namespaces) you refer to. // Example use: // // const { t, tObject } = useTranslation(['football', 'quiz']) // // return
// {t('select')} {/* shorthand now for 'football.select' */} // //
// // You can also use the TemplateStringArray version like: // // {t`select`)} // // Any namespace used when you initialize `useTranslation` that isn't // recognized (prepared in the page props) will throw an error. // And any key not recognized will also throw an error. For example: // // // // ...will throw because of the typo 'sav_changes' instead of 'save_changes'. export const useTranslation = (namespaces: string | Array) => { const { data } = useMainContext() const loadedData = data.ui return createTranslationFunctions(loadedData, namespaces) } /** * Hook for App Router contexts that don't use MainContext */ export const useAppTranslation = (uiData: UIStrings, namespaces: string | Array) => { return createTranslationFunctions(uiData, namespaces) }