0.0.58 • Published 1 year ago

@atomintl/gatsby-plugin-intl v0.0.58

Weekly downloads
10
License
MIT
Repository
-
Last release
1 year ago

gatsby-plugin-intl

Internationalize your Gatsby site.

Features

  • Turn your gatsby site into an internationalization-framework out of the box powered by react-intl.

  • Support automatic redirection based on the user's preferred language in browser provided by browser-lang.

  • Support multi-language url routes in a single page component. This means you don't have to create separate pages such as pages/en/index.js or pages/ko/index.js.

Why?

When you build multilingual sites, Google recommends using different URLs for each language version of a page rather than using cookies or browser settings to adjust the content language on the page. (read more)

Starters

Demo: http://gatsby-starter-default-intl.netlify.com

Source: https://github.com/wiziple/gatsby-plugin-intl/tree/master/examples/gatsby-starter-default-intl

Showcase

Feel free to send us PR to add your project.

How to use

Install package

npm install --save @atomintl/gatsby-plugin-intl

Add a plugin to your gatsby-config.js

// In your gatsby-config.js
plugins: [
  {
    resolve: `@atomintl/gatsby-plugin-intl`,
    options: {
      // language JSON resource path
      path: `${__dirname}/src/intl`,
      // supported language
      // languages: [`en`, `ko`, `de`],
      languages: { "zh-CN": "zh-CN", "en-US": "en-US" },
      // language file path
      defaultLanguage: `ko`,
      // option to redirect to `/ko` when connecting `/`
      redirect: true,
    },
  },
];

You'll also need to add language JSON resources to the project.

For example,

language resource filelanguage
src/intl/en.jsonEnglish
src/intl/ko.jsonKorean
src/intl/de.jsonGerman

Change your components

You can use injectIntl HOC on any react components including page components.

import React from "react";
import {
  injectIntl,
  Link,
  FormattedMessage,
} from "@atomintl/gatsby-plugin-intl";

const IndexPage = ({ intl }) => {
  return (
    <Layout>
      <SEO title={intl.formatMessage({ id: "title" })} />
      <Link to="/page-2/">
        {intl.formatMessage({ id: "go_page2" })}
        {/* OR <FormattedMessage id="go_page2" /> */}
      </Link>
    </Layout>
  );
};
export default injectIntl(IndexPage);

Or you can use the new useIntl hook.

import React from "react";
import { useIntl, Link, FormattedMessage } from "@atomintl/gatsby-plugin-intl";

const IndexPage = () => {
  const intl = useIntl();
  return (
    <Layout>
      <SEO title={intl.formatMessage({ id: "title" })} />
      <Link to="/page-2/">
        {intl.formatMessage({ id: "go_page2" })}
        {/* OR <FormattedMessage id="go_page2" /> */}
      </Link>
    </Layout>
  );
};
export default IndexPage;

How It Works

Let's say you have two pages (index.js and page-2.js) in your pages directory. The plugin will create static pages for every language.

fileEnglishKoreanGermanDefault*
src/pages/index.js/en/ko/de/
src/pages/page-2.js/en/page-2/ko/page-2/de/page-2/page-2

Default Pages and Redirection

If redirect option is true, / or /page-2 will be redirected to the user's preferred language router. e.g) /ko or /ko/page-2. Otherwise, the pages will render defaultLangugage language. You can also specify additional component to be rendered on redirection page by adding redirectComponent option.

Plugin Options

OptionTypeDescription
pathstringlanguage JSON resource path
languagesstring[]supported language keys
defaultLanguagestringdefault language when visiting /page instead of ko/page
redirectbooleanif the value is true, / or /page-2 will be redirected to the user's preferred language router. e.g) /ko or /ko/page-2. Otherwise, the pages will render defaultLangugage language.
redirectComponentstring (optional)additional component file path to be rendered on with a redirection component for SEO.

Components

To make it easy to handle i18n with multi-language url routes, the plugin provides several components.

To use it, simply import it from gatsby-plugin-intl.

ComponentTypeDescription
LinkcomponentThis is a wrapper around @gatsby’s Link component that adds useful enhancements for multi-language routes. All props are passed through to @gatsby’s Link component.
navigatefunctionThis is a wrapper around @gatsby’s navigate function that adds useful enhancements for multi-language routes. All options are passed through to @gatsby’s navigate function.
changeLocalefunctionA function that replaces your locale. changeLocale(locale, to = null)
IntlContextConsumercomponentA context component to get plugin configuration on the component level.
injectIntlcomponenthttps://github.com/yahoo/react-intl/wiki/API#injection-api
FormattedMessagecomponenthttps://github.com/yahoo/react-intl/wiki/Components#string-formatting-components
and more...https://github.com/yahoo/react-intl/wiki/Components

License

MIT © Daewoong Moon

0.0.58-alpha.0

1 year ago

0.0.53

2 years ago

0.0.55

1 year ago

0.0.56

1 year ago

0.0.57

1 year ago

0.0.57-alpha.0

1 year ago

0.0.58

1 year ago

0.0.59-alpha.0

1 year ago

0.0.54-alpha.0

2 years ago

0.0.56-alpha.0

2 years ago

0.0.53-alpha.0

2 years ago

0.0.52

2 years ago

0.0.51

2 years ago

0.0.50

2 years ago

0.0.49

2 years ago

0.0.48

3 years ago

0.0.44

3 years ago

0.0.45

3 years ago

0.0.46

3 years ago

0.0.47

3 years ago

0.0.42

3 years ago

0.0.40

3 years ago

0.0.41

3 years ago

0.0.38

3 years ago

0.0.39

3 years ago

0.0.37

3 years ago

0.0.34

3 years ago

0.0.33

3 years ago

0.0.32

3 years ago

0.0.31

4 years ago

0.0.30

4 years ago

0.0.29

4 years ago

0.0.28

4 years ago

0.0.27

4 years ago

0.0.23

4 years ago

0.0.24

4 years ago

0.0.26

4 years ago

0.0.22

4 years ago

0.0.20

4 years ago

0.0.21

4 years ago

0.0.18

4 years ago

0.0.19

4 years ago

0.0.17

4 years ago

0.0.16

4 years ago

0.0.14

4 years ago

0.0.15

4 years ago

0.0.11

4 years ago

0.0.12

4 years ago

0.0.10

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.7

4 years ago

0.0.6

4 years ago

0.0.5

4 years ago

0.0.3

4 years ago

0.0.4

4 years ago

0.0.2

4 years ago