Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React i18next useTranslation Hook in helper method

I'm using React with react-i18next

My index.tsx File contains some components and I can use the Translation function there

index.js
import React, { Suspense } from 'react'
import ReactDOM from 'react-dom';
import * as utils from './Utils';
import './i18n';
import { useTranslation, withTranslation, Trans } from 'react-i18next';

...
  const { t, i18n } = useTranslation();
  //I can use the translate function here
  t("title");
  //call a util function
  utils.helperFunction(...);
...

Everything works fine here. I now created some helper functions in an additional file

Utils.tsx
...
import { useTranslation, withTranslation, Trans } from 'react-i18next';
...
export function helperFunction(props: any){
   //do stuff

   //now I need some translation here - but useTranslation is not working?
   const { t, i18n } = useTranslation();
   t("needTranslation");
}

How can I use the same translation logic inside my helper function? (without always passing the t function to the helper-method)

Or is the usage of the hook the wrong approach here?

The following error occurs

React Hook "useTranslation" is called in function "helperFunction" which is neither a React function component or a custom React Hook function  react-hooks/rules-of-hooks
like image 906
Evil_skunk Avatar asked Dec 18 '19 15:12

Evil_skunk


People also ask

How do you use withTranslation in React?

Translate a class component using withTranslationImport it at the top with import { withTranslation } from 'react-i18next'; . Then, at the bottom of the file, export it with export default withTranslation()(Home); . Now the t function will be passed into the component's props for use.

How do I import i18next into React?

use(initReactI18next) . init({ resources, lng: "en", interpolation: { escapeValue: false, }, }); export default i18next; import i18next from "i18next"; import { initReactI18next } from "react-i18next"; // "Inline" English and Arabic translations. // We can localize to any language and any number of languages.

How do you use React i18next in react native?

Basic usage import React from 'react' import { Text } from 'react-native' import { useTranslation } from 'react-i18next' const Component: React. FC = () => { const { t } = useTranslation() return <Text>{t('demoScope. title')}</Text> // -> "i18next is Great!" }


1 Answers

I fixed my issue by not using the useTranslation hook anymore

Instead I moved the i18n initalizitation to a file (i18n.tsx - exports i18n) and import and use it in my Utils class

My Utils.tsx file now looks like this

Utils.tsx

...
import i18n from '../i18n';
...
export function helperFunction(props: any){
   //do stuff

   //use imported i18n and call the t() method
   i18n.t("needTranslation");
}

i18n.tsx

import i18n from "i18next";
import Backend from 'i18next-xhr-backend';
import { initReactI18next } from 'react-i18next';

i18n
  .use(Backend) 
  .use(initReactI18next) // passes i18n down to react-i18next
  .init({
    lng: "es",
    fallbackLng: 'en',
    backend: {
      loadPath: '/static/locales/{{lng}}/{{ns}}.json'
    },    
    interpolation: {
      escapeValue: false
    }
  });

  export default i18n;
like image 53
Evil_skunk Avatar answered Sep 26 '22 04:09

Evil_skunk