0

Hello guys i have a question for you. Actually i want to map and render a component using text from a .json and using translation based on user language or detected language.

That's the translation.json (the same for another language)

{ "myText":[
          { "id":1,
            "text":"my text"
          },
          {
            "id":2,
            "text":"my text"
          }
  ]
}

Now i want to map this .json and return the text based on language in my component. If i do:

import { withNamespaces } from 'react-i18next';
import data from '../locales/en/translation.json';

function Component({t}) {
  return (
    <>
      {data.myText.map((item) => (
        <div key={t(item.id)}>
          {t(item.text)}
        <div/>       
    ))}          
    </>
  )
}

export default withNamespaces()(Component)

That's return only the "en" translation because the import is based on this .json file. Now how to have the translation based on user language through .map ?

2

1 Answer 1

1

Ok i did it using:

{i18next.t('myText', { returnObjects: true }).map((item) => (
    <div key={t(item.id)}>
      {t(item.text)}
    </div>
))}

Solution is here: https://www.i18next.com/translation-function/objects-and-arrays#arrays

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.