Skip to content
This repository has been archived by the owner on Dec 27, 2022. It is now read-only.

Fix i18n support #310

Merged
merged 6 commits into from
Mar 24, 2022
Merged

Fix i18n support #310

merged 6 commits into from
Mar 24, 2022

Conversation

remidej
Copy link
Contributor

@remidej remidej commented Mar 22, 2022

What this does:

  • Fix single types internationalization (fixes single type does not merge - can't query all locales #291)
  • Introduce a new pluginOptions object in a content type's config object. Right now it's only to add an i18n key, but the goal is to be able to expand on it later, so that it eventually replaces queryParams altogether

First, I check if a content type has pluginOptions.i18n.locale defined. If it does, for collection types I set that value as the locale query param. For single types types, I do the same, except if the locale is "all", because it's not supported for single types in Strapi.

So in this case, I populate the localizations key in order to get the list of all available locales for a single type. Then, I make a dedicated request for each of these locales, so that they have the same response (level of population etc.) as the base request. I then merge all the localizations together with the default one.

How to test it:

  • Create a Strapi app with the corporate template (because it has localized content)
  • Create a Gatsby app with this config:
module.exports = {
  plugins: [
    {
      resolve: `gatsby-source-strapi`,
      options: {
        apiURL: "http://localhost:1337",
        accessToken: process.env.API_TOKEN,
        collectionTypes: [
          {
            singularName: 'page',
            pluginOptions: {
              i18n: {
                locale: 'fr' // try changing this
              }
            }
          }
        ],
        singleTypes: [
          {
            singularName: 'global',
            pluginOptions: {
              i18n: {
                locale: 'all', // try changing this
              },
            },
          }
        ],
      },
    },
  ],
}
  • Open GraphiQL and query the different localizations of your content types

src/fetch.js Outdated Show resolved Hide resolved
@remidej remidej requested a review from soupette March 22, 2022 16:43
Copy link
Contributor

@markkaylor markkaylor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work! Looks good to me

singularName: 'article',
pluginOptions: {
i18n: {
locale: 'fr', // Only fetch a specific locale
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also provide an array of locales right? Don't know if it's worth mentioning

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's possible, it's either one locale or "all" to fetch everything

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested with an array ["fr", "en"] and seemed to work but I'm not sure if it is defaulting to 'all' or fetching what is specified in the array.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just checked and it seems to work indeed. But I see no mention of this in the Strapi i18n docs, so maybe we shouldn't document it either

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed 👍

@remidej remidej merged commit c72d6c7 into master Mar 24, 2022
@remidej remidej deleted the plugin-options-i18n branch March 24, 2022 08:55
@remidej remidej added this to the 2.0.0 milestone Mar 24, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

single type does not merge - can't query all locales
3 participants