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.

[gatsby-source-wordpress] switching languages using WPML

See original GitHub issue

I’m in the process of converting a multi-language WordPress website using WPML to Gatsby.

Unfortunately I’m not quite sure how to get Gatsby to create pages for every post/page/custom post type in all defined languages automatically and how to set a connection between the languages (e.g. when I’m on a specific page and change the language, display that page in the selected language).

I already installed the WPML-REST-API WP-Plugin which lists the existing translations for each post including their ID but I’m just not sure how and if those IDs can be processed by Gatsby automatically.

Is it necessary to adjust the gatsby-source-wordpress plugin in order to achieve this and if yes, which part would I need to change? I’m thinking of extending the plugin using a config-flag like useWpmlRestApi or something like that.

I’m using react-i18next for translating non-WP content and switching languages if that helps.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
piehcommented, Dec 18, 2017

I’m using Polylang but conceptually this aproach will work with any multi language plugin: in my project gatsby-node i build wordpress_id => path map before creating pages:

const buildLinkMap = ({ getNodes }) => {
  const linkMap = {};

  const typeMap = {
    wordpress__POST: 'post',
    wordpress__PAGE: 'page',
    wordpress__wp_portfolio: 'portfolio',
  };

  for (let node of getNodes()) {
    if (node.internal.type in typeMap) {
      linkMap[node.wordpress_id] = buildLink({
        lang: node.lang,
        slug: node.slug,
        type: typeMap[node.internal.type],
      });
    }
  }
  return linkMap;
};

buildLink is helper function that constructs path based on lang, slug and content type

and then in exports.createPages for each page I get translations, convert translated pages ids to paths and pass that in context and access that data in page component (this.props.pathContext.translations )

0reactions
mlvlestercommented, Jun 24, 2019

Hi @pieh, I’m trying to implement switching languages using Polylang but I can’t find documentation about it. Maybe you could share an example of how you implemented it? Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Create Multilingual Web Pages With Gatsby, WordPress ...
Switching pages doesn't change the language because the WordPress REST API is only giving out data in one language, and we'll have to...
Read more >
Language switch with gatsby and WPML - Stack Overflow
I've recently been trying to create a basic multilingual wordpress website using gatsby. For now, I have been able to fetch my main...
Read more >
How to change the source language of strings - WPML
The page will show you a table of the strings in that domain. You can choose the new language and also make it...
Read more >
Gatsby Changelog | 5.3.0
We intend to document as many ways to use a GraphQL language server to benefits ... millette: chore(gatsby-source-wordpress): Add WPGraphQL WPML PR #34609 ......
Read more >
Headless WordPress development with React using Gatsby.js
3.3.2 Create a new Gatsby project with gatsby-source-wordpress . ... Custom Fields (ACF) entities and Polylang and WPML language information ...
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