Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ewelinam10 opened this issue Jul 16, 2024 · 1 comment

Comments

@ewelinam10
Copy link

ewelinam10 commented Jul 16, 2024

🐛 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

  • runtime version: node v16.11.1, chrome v126.0.6478.127
  • i18next version: "^21.9.1"
  • os: Windows
  • i18n configuration : i18n
    .use(Backend)
    .use(initReactI18next)
    .init({
    backend: {
    loadPath: getCoreAPIUrl('xx.json')
    },
    ns: [
    'animals'
    ],
    defaultNS: 'common',
    lng: LanguageCodes.en_US,
    load: 'currentOnly',
    fallbackLng: LanguageCodes.en_US,
    debug: false,
    interpolation: {
    escapeValue: false
    },
    react: {
    useSuspense: false
    }
    })
@adrai
Copy link
Member

adrai commented Jul 16, 2024

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: #909 #1757

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants