Tuan Pham
Tuan Pham

Reputation: 59

useTranslation not working in react hook?

I have a project write with react hook and I want to change language. I use i18n but when i use useTranslation to change language it load very long and I do not know how to fix it. Pls help me with this and sorry for my bad english.

file routes:

const Routes = () => {
  return (
    <Suspense fallback={<BrandLoading />}>
      <Switch>
        <RouteWithLayout
          component={DashboardView}
          exact
          layout={MainLayout}
          path={`/${routeUrls.dashboard.path}`}
        />
      </Switch>
    </Suspense>
  );
};

export default Routes;

file App.js

import React from 'react';
import { Router } from 'react-router-dom';
import { createBrowserHistory } from 'history';
import { ThemeProvider } from '@material-ui/styles';
import theme from 'theme';
import Routes from 'routes';
import './i18n'

const browserHistory = createBrowserHistory();
const App = () => {
  return (
    <ThemeProvider theme={theme}>
      <Router history={browserHistory}>
        <Routes/>
      </Router>
    </ThemeProvider>
  );
};

export default App;

file i18n.js:

import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import LanguageDetector from 'i18next-browser-languagedetector';
i18n.use(LanguageDetector)
    .use(initReactI18next)
    .init({
     defaultNS: 'translation',
     fallbackLng: 'vn',
     debug: true,
     interpolation: {
       escapeValue: false, 
     },
     load: 'languageOnly',
     saveMissing: true,
   });
export default i18n;

file dashboard.js:

const Dashboard = ({ className = '' }) => {
  const { t, i18n } = useTranslation();
  return (
    <div>
      <Typography>{t.hello}</Typography>
    </div>
  );
};
export default Dashboard;

Upvotes: 2

Views: 12458

Answers (2)

Parham Heydari
Parham Heydari

Reputation: 344

it's a good idea to take a look at the documentations here: https://react.i18next.com/latest/usetranslation-hook

also you need to address your translation files in i18n.js

Upvotes: 0

kubido
kubido

Reputation: 568

it should be t('hello') instead of t.hello since t is a function, not an object

Upvotes: 5

Related Questions