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

Loading translations dynamically from REST API #2

Closed
justintemps opened this issue Jul 17, 2018 · 2 comments
Closed

Loading translations dynamically from REST API #2

justintemps opened this issue Jul 17, 2018 · 2 comments

Comments

@justintemps
Copy link

Here's what I'm trying to accomplish:

Rather than storing my translations in src/18n/locales/*.js, I need to get them from a third party REST API. I'm doing that via sourceNodes in /gatsby-node.js as per this example.

My difficulty is in getting the data from GraphQL to react-intl. Specifically, I can't manage to set messages in the withIntl component, since Gatsby only executes queries in page and layouts components and I was not able to export a fragment from withIntl that Gatsby could find.

I suspect that I cannot accomplish what I'm trying to do with this setup and will instead have to move some of the implementation in withIntl into a layout component that I can provide it with data from GraphQL.

However, before embarking down that road, I thought I'd see if anyone had a better idea.

Thanks very much @wiziple for the awesome starter.

@DigitalGoldfish
Copy link

DigitalGoldfish commented Jul 19, 2018

In my humble opinion you are overthinking the problem doing it in the Gatsby way with GraphQL. I would
just download the translations from the REST API and store them in the /locales/*.js files. It should not be hard to do in gatsby-node.js before any of the pages are created.

@justintemps
Copy link
Author

@DigitalGoldfish You are absolutely right. That's a much better solution.

In the end, I'm grabbing my translations from the REST API in gatsby-node.js using onPreBootstrap and saving them in the /locale/*.js files which is where they want to be. Works like a charm.

Thank you so much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants