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

Question - how to correctly set up react-intl provider with Gatsby? #3792

Closed
szimek opened this issue Jan 31, 2018 · 5 comments
Closed

Question - how to correctly set up react-intl provider with Gatsby? #3792

szimek opened this issue Jan 31, 2018 · 5 comments
Labels
type: question or discussion Issue discussing or asking a question about Gatsby

Comments

@szimek
Copy link
Contributor

szimek commented Jan 31, 2018

I'm trying to add react-intl to my project and when I run gatsby build static pages are generated correctly, but when I load them in the browser, intl object is messed up in template components, i.e. it has incorrect locale and intl.messages object is empty.

My current setup looks like this:

  1. I've got a separate layout for each locale, e.g.
// src/layouts/de.js
import React from 'react';
import Layout from './index';
import messages from '../../data/translations/de.json';

export default (props) => <Layout {...props} locale="de" messages={messages} />;
  1. I'm using IntlProvider in index layout:
// src/layouts/index.js
import React from 'react';
import { IntlProvider } from 'react-intl';

function TemplateWrapper({ children, locale, messages }) {
  return (
    <IntlProvider locale={locale} messages={messages}>
      <div>{children()}</div>
    </IntlProvider>
  );
}

export default TemplateWrapper;
  1. In individual page templates I'm using FormattedMessage and injectIntl.

I've added console.log at each step to see where it gets messed up.

Here are logs when running gatsby build for /de/blog page:

Rendering "de" layout...
path /de/blog
Rendering "index" layout...
path /de/blog
locale de
messages Datenschutz
Rendering "index" page...
locale de
messages Datenschutz

Here are logs when opening /de/blog page in a browser:

component---src-layouts-de-jsx-48cc900e186c41daf6ff.js:3 Rendering "de" layout...
component---src-layouts-de-jsx-48cc900e186c41daf6ff.js:3 path /de/blog/
component---src-layouts-de-jsx-48cc900e186c41daf6ff.js:3 messages Datenschutz
component---src-layouts-de-jsx-48cc900e186c41daf6ff.js:3 Rendering "index" layout...
component---src-layouts-de-jsx-48cc900e186c41daf6ff.js:3 path /de/blog/
component---src-layouts-de-jsx-48cc900e186c41daf6ff.js:3 locale de
component---src-layouts-de-jsx-48cc900e186c41daf6ff.js:3 messages Datenschutz
component---src-templates-index-jsx-66713dbb6ea96235f9fd.js:2 Rendering "index" page...
component---src-templates-index-jsx-66713dbb6ea96235f9fd.js:2 locale en
component---src-templates-index-jsx-66713dbb6ea96235f9fd.js:2 messages undefined

As you can see in the template the data is different when running it in a browser, than in the generated static page.

Any idea how to set it up properly?

@fk fk added the type: question or discussion Issue discussing or asking a question about Gatsby label Jan 31, 2018
@szimek szimek changed the title Question - how to correctly set up react-intl with Gatsby? Question - how to correctly set up react-intl provider with Gatsby? Jan 31, 2018
@szimek
Copy link
Contributor Author

szimek commented Feb 1, 2018

Turns out it had nothing to do with Gatsby and I was simply missing

import { addLocaleData } from 'react-intl';
import localeData from 'react-intl/locale-data/de';
addLocaleData(localeData);

in my layouts 🤦‍♂️

@szimek szimek closed this as completed Feb 1, 2018
@antoinerousseau
Copy link
Contributor

antoinerousseau commented Jan 5, 2019

how do you polyfill Intl for older browsers like IE10?

@szimek
Copy link
Contributor Author

szimek commented Jan 6, 2019

@antoinerousseau I'm using https://polyfill.io. It doesn't always work correctly, as I still sometimes get notifications about missing Intl in older and less common browsers like Samsung Internet 3.x or UC Browser, but it might also be some bug in my code.

I've got the following code in gatsby-browser.js:

exports.onClientEntry = () => {
  /*
   * Polyfills via polyfill.io
   */
  return new Promise((resolve, reject) => {
    // Global callback for polyfill.io script
    // eslint-disable-next-line no-underscore-dangle
    window.__polyfillio__ = () => {
      resolve();
    };

    const features = [];

    if (!('Intl' in window)) {
      const locale = getLocaleFromPath(window.location.pathname);
      features.push(`Intl.~locale.${locale}`);
    }

    if (!('fetch' in window)) {
      features.push('fetch');
    }

    if (!('URL' in window && 'URLSearchParams' in window)) {
      features.push('URL');
    }

    // Use 'always' flag to download polyfills regardless of user agent.
    // We add features to the list only if they are not supported.
    if (features.length) {
      const s = document.createElement('script');
      s.src = `https://cdn.polyfill.io/v2/polyfill.min.js?features=${features.join(
        ',',
      )}&rum=1&flags=always&callback=__polyfillio__`;
      s.async = true;
      s.onerror = reject;
      document.head.appendChild(s);
    } else {
      resolve();
    }
  });
};

@antoinerousseau
Copy link
Contributor

thanks @szimek for sharing your code.
isn't fetch already polyfilled by gatsby?

@szimek
Copy link
Contributor Author

szimek commented Jan 8, 2019

@antoinerousseau I'm not sure. We had this code before we migrated to Gatsby v2.

But it looks like it might not be: zloirock/core-js#25.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question or discussion Issue discussing or asking a question about Gatsby
Projects
None yet
Development

No branches or pull requests

3 participants