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

$i18n.locale behaving differently in templates vs pages #37

Open
adamkendalnfa opened this issue Mar 18, 2021 · 1 comment
Open

$i18n.locale behaving differently in templates vs pages #37

adamkendalnfa opened this issue Mar 18, 2021 · 1 comment
Labels
bug Something isn't working

Comments

@adamkendalnfa
Copy link

Using {{$i18n.locale}} in a page file gives the correct locale. However, using {{$i18n.locale}} in a template file always returns the default locale.

I am using Contentful (@gridsome/source-contentful) to generate template pages with localized data.

Expected behavior
I would expect {{$i18n.locale}} to behave the same in all gridsome file types (layouts, templates, pages).

Environment:

  • Gridsome Version 0.7.0
    -gridsome-plugin-i18n version 1.6.0

gridsome.config.js

module.exports = {
  siteName: "siteName",
  templates: {
    ContentfulContentPage: [{
      path: (node) =>  `/${node.locale === 'en-US' ? 'en' : 'cn'}/${node.slug}`
    }],
  },
  plugins: [
    {
      use: "@gridsome/source-contentful",
      options: {
        space: {{space}}, // required
        accessToken: {{accessToken}}, // required
        typeName: "Contentful",
        locales: ["en-US", "zh-CN"],
      },
    },
    {
      use: "gridsome-plugin-i18n",
      options: {
        locales: [
          // locales list
          "en-US",
          "zh-CN",
        ],
        pathAliases: {
          // path segment alias for each locales
          "en-US": "en",
          "zh-CN": "cn",
        },
        fallbackLocale: "en-US", // fallback language
        defaultLocale: "en-US", // default language
        enablePathRewrite: false, // rewrite path with locale prefix, default: true
        rewriteDefaultLanguage: true, // rewrite default locale, default: true
      },
    },
    {
      use: "@noxify/gridsome-plugin-remote-image",
      options: {
        cache: false,
        typeName: "ContentfulAsset",
        sourceField: "file.url",
        targetField: "localFile",
        targetPath: "src/assets/cf/remoteImage",
      },
    },
  ],
};
@adamkendalnfa adamkendalnfa added the bug Something isn't working label Mar 18, 2021
@andre-m5v
Copy link

I don't have a contentful source ready to go, but I tested with templates generated from both local yaml file and gridsome-source-sanity.

$i18n.locale contains correct values in both. Are you able to put together a repo with minimal app reproducing this issue?

Also take a look at this PR: #32

It has contentful source as an example as well. Could it be specific to this CMS? You can use this PR in your package.json if it fixes your issue as described in comments to this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants