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.

Universal loading with React and Redux

See original GitHub issue

Hello,

I’m trying to create an application with localization that also uses universal loading. I feel like I’m almost there but I’m having an issue with loading the user’s language.

I am using 2 other modules to help me with that: https://github.com/i18next/i18next-node-fs-backend (instead of i18next-xhr-backend in your example) https://github.com/i18next/i18next-express-middleware

Here is what my i18n init looks like:

import i18n from 'i18next'
import Backend from 'i18next-node-fs-backend'
import { LanguageDetector } from 'i18next-express-middleware'

i18n
  .use(Backend)
  .use(LanguageDetector)
  .init({
    whitelist: ['en','en-US','fr'],
    fallbackLng: 'en',

    // have a common namespace used around the full app
    ns: ['common'],
    defaultNS: 'common',

    debug: true,

    interpolation: {
      escapeValue: false // not needed for react!!
    },

    backend: {
      // path where resources get loaded from
      loadPath: 'locales/{{lng}}/{{ns}}.json',

      // path to post missing resources
      addPath: 'locales/{{lng}}/{{ns}}.missing.json',

      // jsonIndent to use when storing json files
      jsonIndent: 2
    }
  })

export default i18n

Then in my express server, I am taking i18n and using it with express so that I can detect the user’s language:

import i18n from './i18n'
import { I18nextProvider } from 'react-i18next'
var i18nMiddleware = require('i18next-express-middleware')

const app = express()
app.use(i18nMiddleware.handle(i18n))

app.get('*', (req, res) => {

  console.log('language ' + req.language)
  console.log('i18n language ' + req.i18n.language)
  console.log(req.i18n.t('app.appName'))

  match({ routes, location: req.url }, (err, redirect, props) => {
      const contentHtml = ReactServer.renderToString(
        <I18nextProvider i18n={ req.i18n }>
          <Provider store={store} key="provider">
            <RouterContext {...props} />
          </Provider>
        </I18nextProvider>
      )
      res.send(layout(contentHtml, store.getState()))
  })
})

I am able to grab req.language and req.i18n.language. Also, req.i18n.t() works for the keys I pass into it. The problem is when I try and pass in req.i18n into the I18nextProvider, it throws an error saying i18n.loadNamespaces is not a function.

Do you have any other suggestions about how to use this? Or maybe there’s an attribute I can add to I18nextProvider for the language to use for the user? Maybe something like this:

<I18nextProvider i18n={ i18n } language={req.language}>

Or maybe I’m going about this all wrong and you have another idea for Universally loading a React/Redux application with i18next capabilities.

Thanks for your help!

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:19 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
jamuhlcommented, Mar 16, 2016

basically by adding all namespaces used in client to the ns: [‘common’] on server, plus adding preload: [‘en’,‘en-US’,‘fr’] to init options

0reactions
loudwinstoncommented, Apr 1, 2016

@jamuhl Thanks! Glad to help out.

Read more comments on GitHub >

github_iconTop Results From Across the Web

erikras/react-redux-universal-hot-example
This is a starter boilerplate app I've put together using the following technologies: Isomorphic Universal rendering; Both client and server make calls to...
Read more >
Lazy Load Redux Reducers with Code-split Universal ...
When you async load a code-split component, sometimes it has associated redux actions and reducers that come bundled with it. We'll explore a...
Read more >
react-redux-universal
Start using react-redux-universal in your project by running `npm i react-redux-universal`. There are no other projects in the npm registry ...
Read more >
7 ways to control loading state in a React app
Let's also assume we are pretty knowledgeable in Redux — generally will speak in Redux lingo, most concepts can be abstracted away and...
Read more >
Building a Universal Higher-Order Component Page Loader ...
The code for the loading HOC isn't very complicated, in actuality. Here's a code snippet of the universal loader component my team uses...
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