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.

Fix concurrency issues due to i18n singleton

See original GitHub issue

We have a problem that is around since the examples of with-i18next on next.js repository which we should try to fix because is really bad issue.

The problem is that we have one instance of i18next and we pass it to the provider

<I18nextProvider
  i18n={i18n}
  initialLanguage={initialLanguage}
  initialI18nStore={initialI18nStore}
>

What happens here is that if a request hits the server and change the language before next renders the html it will render with the wrong language.

I solved the problem by cloning the i18next instance before passing to the provider

...
render() {
  const clone = i18n.cloneInstance({
    initImmediate: false,
    lng: initialLanguage,
  })

  return (
    ...
    <I18nextProvider
      i18n={clone}
      initialLanguage={initialLanguage}
      initialI18nStore={initialI18nStore}
    >
    ...
  )
}

This also happens if you use the i18n instance outside of react context ( and this problem I couldn’t think how to solve it yet ). For example:

I have a file where I setup our apollo-client instance, and there I have some middlewares which inject some headers like this:

import i18n from '../i18n' // <--- the file which exports the i18next instance
...

// Accept language link
const acceptLanguageLink = new ApolloLink((operation, forward) => {
  operation.setContext(({ headers = {} }) => {
    return {
      headers: {
        ...headers,
        'Accept-Language': i18n.language, // <--- Prone to concurrency issue
      },
    }
  })

  return forward(operation)
})

This issue is much more complex than looks like and I would love to think in a solution with you guys however I have some questions about the next-18next setup which @capellini or @isaachinman could give me some insights.

I’ll start with:

  1. What is the context of this here: https://github.com/isaachinman/next-i18next/blob/master/src/hocs/app-with-translation.js#L12. I’m asking to know where the i18n comes from.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

0reactions
isaachinmancommented, Jan 31, 2019

Released in v0.24.0.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fix concurrency issues due to i18n singleton #135 - GitHub
My solution was all over my application use the i18n instance injected by the withNamespaces HOC. Which injects to the component the instance ......
Read more >
Singleton double-check concurrency issue - Stack Overflow
Returning null is not the issue. The issue is that the new instance may be in a partially constructed state as perceived by...
Read more >
i18n'em all with Singleton - 50 Shades of Pink by KBDev
This article will explain how to build a simple translation system (or i18n module) for all your application texts.
Read more >
Ruby concurrency is hard: how I became a Ruby on Rails ...
1 Ephemeral bug from a test-suite · 2 Process shutdown coordination · 3 Pinpointing the issue · 4 Reproduction · 5 Fixing the...
Read more >
i18n | Yarn - Package Manager
i18n. Lightweight simple translation module with dynamic JSON storage. Supports plain vanilla Node.js apps and should work with any framework (like Express, ...
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