npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gatsby-theme-thepuzzlers-intl

v1.1.1

Published

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.

Downloads

18

Readme

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

| Option | Value | Description | Required | | --------------- | ---------------- | ------------------------------------------------------------ | -------- | | locales | Object | Specifies the default locale, as well as the pathPrefix and hreflang of the other supported languages. | Yes | | translatedPaths | Object[] | This is used to get the translation of the page slug name. | Yes | | wrapProvider | Boolean | Default 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 | | secondPath | String | This option is used to combine the translation messages. Add the path to the second translation dir. | No |

Plugin Options from gatsby-plugin-intl

| Option | Value | Description | Required | | --------------- | ---------------- | ------------------------------------------------------------ | -------- | | path | String | The Language JSON resource path | Yes | | languages | String[ ] | List of the supported languages. Ex. languages: ["en", "de"] | Yes | | redirect | Boolean | Option 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 name | Value | Description | Required | | --------------- | ---------------- | ------------------------------------------------------------ | -------- | | pathToRevert | String | The 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 name | Value | Description | Required | | --------------- | ---------------- | ------------------------------------------------------------ | -------- | | path | String | The base path (which equal the name of the page in the pages directory), that you want to localize. | Yes | | locale | String | Locale 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)} />
  )
}