1.1.1 • Published 7 months ago

gatsby-theme-thepuzzlers-intl v1.1.1

Weekly downloads
-
License
-
Repository
-
Last release
7 months ago

Gatsby Theme ThePuzzlers Intl

This theme extends gatsby-plugin-react-intl. It is helpful to go through their docs as well to understand the set up: https://www.npmjs.com/package/gatsby-plugin-react-intl.

Getting Started

  1. Install the gatsby theme
    $ yarn add gatsby-theme-thepuzzlers-intl
  1. Add to list of plugins in the gatsby-config file and add the required theme options. Below is an example of the config for german and english pages.
    module.exports = {
        plugins: [{
            resolve: 'gatsby-theme-thepuzzlers-intl',
            options: {
                // react-intl Plugin Options
                path: `${__dirname}/src/intl`, 
                languages: ['en', 'de'],  
                redirect: true,
                // ThePuzzlers Theme Options
                locales: {
                    default: 'en',
                    translations: [{ pathPrefix: 'de', hreflang: 'de' }]
                },
                translatedPaths: [
                    { default: 'privacy-policy', de: 'datenschutz' },
                    { default: 'legal', de: 'impressum', en: 'legal-terms' }
                ]           
                
            }
        }]
    }
  1. Add language JSON resources to the src/intl directory. The file name should be the corresponding language code.

src/intl/en.json

    {
        "title": "Gatsby English", 
        "hello": "Hi people",
        "welcome": "Welcome to your new Gatsby site.",
        "title_page2": "Page two",
        "hello_page2": "Hi from the second page", 
        "go_page2": "Go to page 2",
    }

src/intl/de.json

    {
        "title": "Gatsby Deutsch", 
        "hello": "hallo Leute",
        "welcome": "Willkommen auf Ihrer neuen Gatsby-Site.",
        "title_page2": "Seite zwei",
        "hello_page2": "Hallo von der zweiten Seite",
        "go_page2": "Gehen Sie zu Seite 2", 
    }

Theme Options

ThePuzzlers Theme Options

OptionValueDescriptionRequired
localesObjectSpecifies the default locale, as well as the pathPrefix and hreflang of the other supported languages.Yes
translatedPathsObject[]This is used to get the translation of the page slug name.Yes
wrapProviderBooleanDefault is true. If true, it will wrap the intl providers at the root element. If false, it will not and you are responsible for adding it.No
secondPathStringThis option is used to combine the translation messages. Add the path to the second translation dir.No

Plugin Options from gatsby-plugin-intl

OptionValueDescriptionRequired
pathStringThe Language JSON resource pathYes
languagesString List of the supported languages. Ex. languages: ["en", "de"]Yes
redirectBooleanOption to redirect to /en when connecting /. Default is false.No

Dev Tools

Packages needed for extraction tools

If you are adding gatsby-theme-thepuzzlers-intl these are the packages you can add in order to extract and compile tranlsation messages.

  1. Install Packages
yarn add --dev @babel/core @formatjs/cli babel-plugin-formatjs react-intl-translations-manager
  1. Add babel configuration
// babel.config.json
{
  "plugins": [
    [
      "formatjs",
      {
        "idInterpolationPattern": "[sha512:contenthash:base64:6]",
        "ast": true
      }
    ]
  ],
  "presets": [
    [
      "babel-preset-gatsby",
      {
        "targets": {
          "browsers": [">0.25%", "not dead"]
        }
      }
    ]
  ]
}
  1. Add translationRunner.js: This is to config react-intl-translations-manager
const manageTranslations = require('react-intl-translations-manager').default;

manageTranslations({
  messagesDirectory: 'intl/messages',
  translationsDirectory: 'intl/',
  languages: ['en', 'de'],
  singleMessagesFile: true,
  overrideCoreMethods: {
    provideExtractedMessages: () => {
      const extractedMessages = require('./intl/messages/messages.json');

      // Maps extracted messages to the return value structure they want
      return [
        {
          descriptors: Object.entries(extractedMessages).map((entry) => ({
            id: entry[0],
            defaultMessage: entry[1].defaultMessage
          }))
        }
      ];
    }
  }
});
  1. Add scripts
  "scripts": {
    "extract": "formatjs extract \"src/**/*.js*\" --out-file intl/messages/messages.json --flatten --id-interpolation-pattern '[sha512:contenthash:base64:6]'",
    "compile": "node ./translationRunner.js"
  },

How To Extract & Compile translations

  1. Run yarn extract command to extract all the translations within src/ folder
  2. Run yarn compile command to generate updated language json files (ie. en.json & de.json)

Exported helper functions

useRevertPathToDefaultLocale

A hook used to revert any translations or prefixes added to a path.

Important note: The returned reverted path does not always equal the path of the default locale. Sometimes the default locale might have a translation configured. Therefore you should always use this hook in combination with the useLocalizePath hook when creating e.g. a link to navigate to.

Arguments

Arg nameValueDescriptionRequired
pathToRevertStringThe path that you want to strip of all translations and path prefixes.Yes

Example usage

import { useRevertPathToDefaultLocale } from `gatsby-theme-thepuzzlers-intl`;

const MyComponent = () => {
  const basePath = useRevertPathToDefaultLocale('/de/datenschutz')

  return (
    <div />
  )
}

useLocalizePath

A hook used to revert any translations or prefixes added to a path.

Arguments

Arg nameValueDescriptionRequired
pathStringThe base path (which equal the name of the page in the pages directory), that you want to localize.Yes
localeStringLocale you want to localize your component to.Yes

Example usage

import { useLocalizePath } from `gatsby-theme-thepuzzlers-intl`;

const MyComponent = () => {
  const localizedPath = useLocalizePath('privacy-policy', 'de')  // '/de/datenschutz'

  return (
    <button onClick={() => navigate(localizedPath)} />
  )
}
1.1.1

7 months ago

1.1.0

8 months ago

1.0.0

1 year ago

0.13.1

2 years ago

0.13.5

1 year ago

0.12.7

2 years ago

0.12.6

2 years ago

0.10.10

2 years ago

0.9.7

2 years ago

0.9.4

2 years ago

0.9.3

2 years ago

0.9.5

2 years ago

0.9.0

2 years ago

0.9.1

2 years ago

0.7.0

2 years ago

0.5.6

2 years ago

0.4.19

2 years ago

0.4.17

2 years ago

0.4.18

2 years ago

0.4.13

2 years ago

0.4.14

2 years ago

0.4.11

2 years ago

0.4.12

2 years ago

0.5.0

2 years ago

0.4.3

3 years ago

0.4.0

3 years ago

0.3.0

3 years ago

0.2.41

3 years ago

0.2.40

3 years ago

0.2.27

3 years ago

0.2.26

3 years ago

0.2.47

3 years ago

0.2.46

3 years ago

0.2.45

3 years ago

0.2.44

3 years ago

0.2.43

3 years ago

0.2.21

3 years ago

0.2.42

3 years ago

0.2.39

3 years ago

0.2.37

3 years ago

0.2.36

3 years ago

0.2.35

3 years ago

0.2.34

3 years ago

0.2.33

3 years ago

0.2.29

3 years ago

0.2.28

3 years ago

0.2.17

3 years ago

0.2.14

3 years ago

0.2.12

3 years ago

0.2.9

3 years ago

0.2.0

3 years ago

0.1.3

3 years ago

0.1.1

3 years ago