i18next / react-i18next

Internationalization for react done right. Using the i18next i18n ecosystem.
https://react.i18next.com
MIT License
9.32k stars 1.03k forks source link

Translations stop working when sharing between to files (one no-component one) #1771

Open ewelinam10 opened 4 months ago

ewelinam10 commented 4 months ago

🐛 Bug Report

Hello, I am facing weird bug. When I share array that contains i.a. translations between two files ( one file with component, second file with service) , those translations stop working. When I remove usage from service, and use only in one file, it is fine. Also it is working when I've change scope of this array by changing declaration from const to var. Maybe is it related to some bundling ?

To Reproduce

animal.component.tsx

export const items = [
  {
    id: 1,
    value: 'cat',
    label: t('animals:cat')
  },
  {
    id: 2,
    value: 'dog',
    label: t('animals:dog')
  }
]
export const Animal: React.VFC = () => {
  return <>{items[0].label}</>
}

animal.service.tsx

export class AnimalService {
  public static getAnimalDetails() {
    //some API request
    return items[0].value
  }
}

Expected behavior

Animal component shows translation for cat

Your Environment

adrai commented 4 months ago

Please provide a minimal reproducible example repository or codesandbox example etc... But looking at your animal.component.tsx code, it seems you are using the t() function outside of a react component? The t function needs to be executed inside a function (normally where the UI is rendered...)

maybe related: https://github.com/i18next/react-i18next/issues/909 https://github.com/i18next/react-i18next/issues/1757