Git Product home page Git Product logo

gatsby-plugin-transform-i18n-locales's Introduction

gatsby-plugin-transform-i18n-locales

Creates GraphQL nodes from i18n locale files. Uses gatsby-source-filesystem to enable HMR for development.

How to use

  1. Add gatsby-local-plugin-transform-i18n-locales as a plugin entry inside gatsby-config.js.
  2. Add a new entry for gatsby-source-filesystem inside gatsby-config.js. Point the path entry to a folder that contains your i18n locale files.
  3. Query nodes from Gatsby pages.
  4. You can either manually enter language per page query or create pages programatically with Gatsby's createPages API and pass a lang key for pageContext that you can then use to query the correct locale for the current page.
plugins: [
  `gatsby-local-plugin-transform-i18n-locales`,
  {
    resolve: `gatsby-source-filesystem`,
    options: {
      name: `i18n-locales`,
      path: `${__dirname}/locales/`
    }
  }
]

i18n locales format

Create a file per language locale inside your locales folder.

  • locales/en.json
  • locales/de.json
  • ...
{
  "namespace": {
    "someKey": "This is some value",
    "keyWithStringInterpolation": "This is a value with a variable of {{variable}}",
    "variable": 3
  },
  "anotherNamespace": {
    "someKey": "This is some key",
    "keyWithStringInterpolation": "This is a value with a variable of {{variable}}"
  }
}

Querying locale data from Gatsby pages

export const TranslationsQuery = graphql`
  query($lang: String!, $namespaces: [String!]) {
    i18n: allI18NNamespaces(
      filter: { lang: { eq: $lang }, namespace: { in: $namespaces } }
    ) {
      nodes {
        namespace
        allTranslations
      }
    }
    i18nAdditions: allI18NNamespaces(
      filter: {
        lang: { eq: $lang }
        # Query additional translation strings here
        namespace: { in: ["workday-services", "pre-deployment"] }
      }
    ) {
      nodes {
        namespace
        singleTranslations {
          title
        }
      }
    }
    i18nStatic: allI18NNamespaces(
      filter: { namespace: { in: ["common", "privacy-notice"] } }
    ) {
      nodes {
        lang
        namespace
        allTranslations
      }
    }
  }
`

gatsby-plugin-transform-i18n-locales's People

Watchers

Mikko Vänskä avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.