• Stars
    star
    434
  • Rank 100,274 (Top 2 %)
  • Language
    JavaScript
  • Created over 7 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Multi language routes for Gatsby

gatsby-plugin-i18n

Codacy Badge Build Status NPM codecov.io Dependency Status MIT license

Hi Folks!

Are you trying to build a multi language gatsby website?

We want to help you! Please open an issue for help, suggestions or bugs.

You can use this plugin with react-intl, i18next, or any other i18n library. This plugin do not translate messages, it just creates routes for each language, and you can use different layouts for each language if you want to.

How it works

Name your files with .langKey.js and the url will be /langKey/path/fileName

Examples:

file url
src/pages/about.en.js /en/about
src/pages/about/index.en.js /en/about
src/pages/blog/gatsby-i18n.pt.md /pt/blog/gatsby-i18n

Why?

Google! Google needs different URLs to crawl and render your pages for each language.

Showcase

Websites built with Gatsby i18n:

Feel free to add your project to the list, we would love to see what you are building!

Starters

Docs

  • gatsby-starter-default-i18n DEMO, features:
    • automatic browser-language detection and redirection
    • integration with react-intl FormattedMessage with translation keys
    • custom layout and pages per language
    • language switcher component ย  - dev mode with HMR
    • build deployed to Netlify

Packages

Install

  yarn add gatsby-plugin-i18n

How to use

Include the plugin in your gatsby-config.js file.

Simple configuration example:

// Add to gatsby-config.js
plugins: [
  {
    resolve: 'gatsby-plugin-i18n',
    options: {        
      langKeyDefault: 'en',
      useLangKeyLayout: false
    }
  }
]

Blog using markdownRemark configuration example:

// Add to gatsby-config.js
plugins: [
  {
    resolve: 'gatsby-plugin-i18n',
    options: {        
      langKeyDefault: 'en',
      useLangKeyLayout: false,
      markdownRemark: {
        postPage: 'src/templates/blog-post.js',
        query: `
          {
            allMarkdownRemark {
              edges {
                node {
                  fields {
                    slug,
                    langKey
                  }
                }
              }
            }
          }
        `
      }
    }
  }
]

All Options

  • langKeyDefault: langKey to use when no langKey specified.

  • useLangKeyLayout:

    true: use a different layout for each langKey (src/layouts/en.js, src/layouts/pt.js, ...)

    false: use default layout (src/layouts/index.js)

  • markdownRemark:

    Add markdownRemark if you are using gatsby-transformer-remark.

    You can set a postPage component and a query to get the pages.

  • langKeyForNull: langKey added to page context and graphql when no langKey specified. Default: any.

  • pagesPaths: If you are not using just /src/pages/ folder, you can add an array with the folders your are using:

     plugins: [
       //... other plugins
       {
         resolve: 'gatsby-plugin-i18n',
         options: {        
           //.. other options
           pagesPaths: [ '/my/custom/pages/folder1', /my/custom/pages/folder2/ ]
         }
       }
     ]
    
  • prefixDefault:

    true: add langKey on all pages, including default

    false: omit langKey in url when page lang is the default. Ex: when langKeyDefault is en, blog/first-post.en.md and blog/first-post.pt.md will have the following urls:

    • /blog/first-post
    • /pt/blog/first-post

    Default: true

Finally

Go Gatsby!

Go Open-source!

Good luck folks! Open an issue if you need help.

More Repositories

1

angeloocana

i18n React static site generated with Gatsby. Learn Linux, js, graphql and much more at angeloocana.com
JavaScript
32
star
2

tic-tac-toe-ai

HTML5 Canvas + Bitwise + Neural networks learning how to play tic tac toe
JavaScript
32
star
3

CursoProjetoCompletoMVC

Curso Projeto Completo MVC Youtube
C#
20
star
4

gatsby-starter-default-i18n

The default Gatsby i18n (Multilanguage) starter
CSS
17
star
5

ptz-i18n

lib for i18n functions, used in gatsby projects with gatsby-plugin-i18n
JavaScript
10
star
6

freecomclub

Free Community Club (freecom.club) is an opensouce project to create free democracy, where every citizen can vote and keep track to every proposal.
TypeScript
7
star
7

docker-alpine-nodejs-mongodb

Docker image for Alpine, Nodejs and MongoDB
5
star
8

ptz-math

ptz-math is a javascript library for math operations
TypeScript
3
star
9

joj-core

JumpOverJump.com core library
TypeScript
3
star
10

ptz-assert

ptz-assert is a wrapper for npm assert
TypeScript
3
star
11

arch-home

Vim, ZSH, ... configuration files
Vim Script
2
star
12

moon-nodes

Calculate ascending and descending moon between 2001 and 2100
JavaScript
2
star
13

gatsby-plugin-i18n-tags

i18n tags for gatsby
JavaScript
2
star
14

gatsby-plugin-i18n-readnext

Read Next feature for multi language gatsby projects.
JavaScript
2
star
15

ptz-copy

ptz-copy is a javascript library for coping objects
TypeScript
2
star
16

clojure-exercises

Clojure exercises and examples
Clojure
1
star
17

docker-alpine-nodejs

Base image for a docker alpine nodejs envirioment
1
star
18

tic-tac-toe-ai-clojure

ClojureScript Tic Tac Toe AI
Clojure
1
star
19

DonatellaPG

Projeto teste Pizzaria Donatella
JavaScript
1
star