UNPKG

ra-core

Version:

Core components of react-admin, a frontend Framework for building admin applications on top of REST services, using ES6, React

37 lines (33 loc) 1.11 kB
import { useCallback } from 'react'; import { TranslateFunction } from '../types'; import { useI18nProvider } from './useI18nProvider'; /** * Translate a string using the current locale and the translations from the i18nProvider * * @see Polyglot.t() * @link https://airbnb.io/polyglot.js/#polyglotprototypetkey-interpolationoptions * * @return {Function} A translation function, accepting two arguments * - a string used as key in the translations * - an interpolationOptions object * * @example * * import { useTranslate } from 'react-admin'; * * const SettingsMenu = () => { * const translate = useTranslate(); * return <MenuItem>{translate('settings')}</MenuItem>; * } */ export const useTranslate = (): TranslateFunction => { const i18nProvider = useI18nProvider(); const translate = useCallback( (key: string, options?: any) => i18nProvider.translate(key, options) as string, // update the hook each time the locale changes [i18nProvider] ); return i18nProvider ? translate : identity; }; const identity = key => key;