use-wait-for-i18n.ts 581 B

1234567891011121314151617181920212223242526
  1. import { useEffect, useState } from 'react'
  2. import i18n from '@/i18n'
  3. import { useTranslation } from 'react-i18next'
  4. function useWaitForI18n() {
  5. const { ready: isHookReady } = useTranslation()
  6. const [isLocaleDataLoaded, setIsLocaleDataLoaded] = useState(false)
  7. const [error, setError] = useState<Error>()
  8. useEffect(() => {
  9. i18n
  10. .then(() => {
  11. setIsLocaleDataLoaded(true)
  12. })
  13. .catch(error => {
  14. setError(error)
  15. })
  16. }, [])
  17. return {
  18. isReady: isHookReady && isLocaleDataLoaded,
  19. error,
  20. }
  21. }
  22. export default useWaitForI18n