question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Translation outside React Component

See original GitHub issue

I tried to translate strings exported outside React Components. But any methods in docs doesn’t work, and I guess its normal, because its typically React methods.

I’ve found similar topic, and there it is solution about importing instance. But, when I do it I’ve had an error:

 Invariant Violation: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
See https://fb.me/react-invalid-hook-call for tips about how to debug and fix this problem.

Probably it because of this line in the instance:

i18n
    .use(initReactI18next) 

When I delete this line, the error is gone, but the translations are not included. How can I plug this simple .js file to React translations?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:26 (11 by maintainers)

github_iconTop GitHub Comments

104reactions
jamuhlcommented, Jul 24, 2019

Outside of react components just use the i18next instance:

import i18n from `./i18n`

i18n.t(...);

Just be aware of the normal catch you’s:

20reactions
jamuhlcommented, Jul 26, 2019

i18n.t returns a string — right?

export default { foo: 'bar' } — exports an object with a string foo — right? export default { foo: i18n.t('foo') } — still exports an object with a string foo — right?

how does a string magically changes? - it does not — right?

So this might work?

import { i18n } from './i18n';

const myExport = {};

function fill() {
  myExport.foo = i18n.t('foo');
}

// run it initial
fill();

// bind some events and fill values again (doing the magic you expect to happen magically)
i18n.on('languageChanged init',() => {
  fill(); // fills myExport with new value for foo based on current i18n lng
});

// export the const
export default myExport;
Read more comments on GitHub >

github_iconTop Results From Across the Web

i18next translation outside component - Stack Overflow
Show activity on this post. I'm new for i18next, trying to localize/translate website. Everything works for translation inside of component, ...
Read more >
Step by step guide - react-i18next documentation
The Trans component is the best way to translate a JSX tree in one translation. This enables you to eg. easily translate text...
Read more >
Common i18n patterns in React — LinguiJS documentation
Translations outside React components ¶ · Create your own instance of i18n using setupI18n() form @lingui/core · Pass this instance as i18n prop...
Read more >
How to use Translation outside a React component?
Hi, I'm trying to add translation to a object that is outside a React Component, is this possible?
Read more >
Common i18n patterns in React — LinguiJS documentation
Using jsx macros is the most straightforward way how to translate your React components. <Trans> handles translations of messages including variables and other ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found