import { useContext , useCallback } from 'react' ; import { TranslationContext } from './TranslationContext' ; import { useUpdateLoading } from '../loading' ; import { useNotify } from '../sideEffect' ; example const useSetLocale = ( ) : SetLocale => { const { setLocale , i18nProvider } = useContext ( TranslationContext ) ; const { startLoading , stopLoading } = useUpdateLoading ( ) ; const notify = useNotify ( ) ; return useCallback ( ( newLocale ) => new Promise ( resolve => { startLoading ( ) ; resolve ( i18nProvider . changeLocale ( newLocale ) ) ; } ) . then ( ( ) => { stopLoading ( ) ; setLocale ( newLocale ) ; } ) . catch ( error => { stopLoading ( ) ; notify ( 'ra.notification.i18n_error' , { type : 'warning' } ) ; console . error ( error ) ; } ) , [ i18nProvider , notify , setLocale , startLoading , stopLoading ] ) ; } ; type SetLocale = ( locale : String ) => Promise < void > ; export default useSetLocale ;