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-localization

v1.5.1

Published

Full solution for Gatsby localization

Downloads

172

Readme

gatsby-theme-localization

Opinionated Gatsby theme which provides a full solution for localization in Gatsby. This theme is built upon the awesome gatsby-plugin-i18n and react-i18next and acts as a bridge between the two. I couldn't find a full solution for localization with Gatsby which works the way I want it to, so I made a custom solution for my personal website and put everything I learned in this Gatsby theme in hopes to help out anyone who may struggle with the same.

Documentation

This is a basic overview to get you started. For more advanced features or better documentation, please visit the full documentation.

Getting started

Using this plugin is really simple. First, install it by using npm install gatsby-theme-localization. Add this to your gatsby-config.js:

module.exports = {
  plugins: [
    {
      resolve: `gatsby-theme-localization`,
      options: {
        languages: ['en', 'bg'],
        namespaces: ['translation', 'about'],
        localesDir: './src/locales',
        allowIndex: false,
        defaultLng: 'en',
        i18next: {
          // whatever you want to pass to react-i18next
          fallbackLng: 'en',
          debug: process.env.NODE_ENV !== 'production'
        },
        i18nPlugin: {
          // whatever you want to pass to gatsby-plugin-i18n
          langKeyDefault: 'en',
          useLangKeyLayout: false
        }
      }
    }
  ]
};

This is an example configuration for this plugin, but you can probably get away with even less.

That's it! Your Gatsby-powered website now has localization!

How to use

Currently, the way to store your translations is in a JSON file (although in the future I want to add remote translations).

For each language your page has, create a file in pages following this pattern: [your page name].[language].js. An example of this is index.en.js. Inside your page component, you can use all of react-i18next's features. For me, the most convenient way is to use the useTranslation hook. If your page is the same for all languages, you can just reexport your page component in all languages you want, although this is often not the case.

This theme exports a drop-in replacement for Gatsby's link. In fact, it forwards all props to the original Gatsby link, except for the "to" prop, which is enhanced to prefix the current language. This feature can be disabled via the "prefixLanguage" prop.

In your locales folder, you need to create a folder for each language you support, and a JSON file for each namespace you want. It is important that all files exist, otherwise it will crash. This is intentional as I think it is better to crash your build rather than have missing texts.

How it works

gatsby-plugin-i18n creates the routes for each language but doesn't do anything for the actual translations, so we are free to use any plugin we like. I18next handles the actual translation. My theme synchronizes the two awesome libraries. Make sure to read both plugins' documentation to make use of their full capabilities.

Options API

For example usage, refer to the example gatsby-config.

| Option | Default | Description | |------------|-----------------|--------------------------------------------------------------------------------------------------------------------| | languages | [] | List of language you support | | namespaces | [] | List of namespaces you support | | allowIndex | false | This controls whether you want your "/" route to be accessible. If not, redirect to the user's preferred language | | defaultLng | 'en' | Default language, used for a number of things (such as fallback when language cannot be detected) | | localesDir | './src/locales' | Directory where your JSON files are stored (relative to the root of your project) | | i18next | {} | Object with options which will be passed to i18next | | i18nPlugin | {} | Object with options which will be passed to gatsby-plugin-i18n | | suspenseFallback | undefined | Path to fallback component for Suspense |

Exports

Link

| Prop | Default | Description | |-------------------|---------|-----------------------------------------------------------| | prefixLanguage | true | Prefix language before url (e.g. "/about" -> "/en/about") | | preloadNamespaces | [] | Namespaces to preload on hover for faster loading |

License

Licensed under the MIT License.