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.

Access translation function outside of hook

See original GitHub issue

I’m calling the translation from a callback (below) and keep getting the hook being used incorrectly error. I understand hooks need to be called from functional components but was wondering if translation can be called a different way.

const client = new ApolloClient({
  link,
  cache
});
const link = errorLink.concat(
  ApolloLink.from([stateLink, AuthLink, splitlink])
);

const errorLink = onError(
  ({ graphQLErrors, networkError, response, operation, forward }) => {
    if (graphQLErrors) {
      graphQLErrors.map(({ message, path }) => {
          **const [t] = useTranslation();
          console.log(t(message));**
        }

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
jamuhlcommented, Jun 15, 2019

loading is async…you need to wait for the namespace loaded

either using `i18n.init(opts, () => { /* ready here */ }``

or in a own file:

import i18n from './i18n';

i18n.loadNamespace('common', () => { i18n.t('common:key'); });

or using some event: https://www.i18next.com/overview/api#events

0reactions
jamuhlcommented, Jun 16, 2019

for 2) it is loadNamespaces - sorry for the typo: https://www.i18next.com/overview/api#loadnamespaces

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use translation i18n in React without hook?
React Hook "useTranslation" is called in function "myFunction: Middleware<{}, RootStateType>" which is neither a React function component or a ...
Read more >
useTranslation (hook) - react-i18next documentation
Use the useTranslation hook inside your functional components to access the translation function or i18n instance. useTranslation params ...
Read more >
A Guide to React Localization with i18next | Phrase
We access t() via react-i18next's useTranslation() react hook. The hook ensures that our components get the t() associated with our i18next instance.
Read more >
Hooks FAQ - React
You can try moving that function outside of your component. In that case, the function is guaranteed to not reference any props or...
Read more >
Build your own useTranslation hook with Next.js
Now the core idea of this useTranslation hooks is the t function: it receives a key and access the TRANSLATIONS object using that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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