import { IGetLocalizedValueOptions, ILocalizedValue, getLocalizedValue } from '@/utils'; import { useLocaleState } from 'ra-core'; import { useCallback } from 'react'; function useLocale(locale?: string): string { const [defaultLocale] = useLocaleState(); return locale !== undefined ? String(locale) : defaultLocale; } type IUseLocalizeValueOptions = IGetLocalizedValueOptions & { locale?: string; }; function useLocalizedValue(value: ILocalizedValue, options?: IUseLocalizeValueOptions): T | undefined { const language = useLocale(options?.locale); const localizedValue = getLocalizedValue(value, language, options); return localizedValue; } type IUseGetLocalizeValueOptions = IUseLocalizeValueOptions; function useGetLocalizedValue(options?: IUseGetLocalizeValueOptions): (v: ILocalizedValue) => T | undefined { const language = useLocale(options?.locale); const callback = useCallback( (value: ILocalizedValue) => { return getLocalizedValue(value, language, options); }, [language, options] ); return callback; } export { useGetLocalizedValue, useLocalizedValue };