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

@3nvi/gatsby-theme-intl

v0.4.20

Published

A theme to automate the mundane tasks of translation & localization of static pages

Downloads

122

Readme

gatsby-theme-intl

Helps with i18n by creating pages & handling translations for all your supported locales.

Features

In short, this does everything gatsby-plugin-intl does, while exposing
React Components to help you handle translations and other internalization chores. It:

  • Creates a page for each of your locales and exposes them through different URLs
  • Sets the locale for this page and exposes it through React.Context to any component within the page
  • Creates all the necessary SEO tags for each of your new localized pages
  • Creates proper redirects based on Language headers, as well as the default/fallback language

At its core, this plugin exposes the following:

usePageContext

Returns information about the current page.

import { usePageContext } from '@3nvi/gatsby-theme-intl';

const Component = () => {
  const {
    // The language of the current page (i.e. `en`)
    lang,
    // The original path of the page before it became localized (i.e. `/about`)
    originalPath,
    // The supported languages of your application (i.e. `['en']`)
    supportedLanguages,
    //The URL of your current site (i.e `http://localhost:8000`)
    siteUrl,
  } = usePageContext();

  return <div />;
};

useTranslation

Returns a helper function for translations. This package uses & configures i18next under the hood, so you can read more there about how to configure your translations.

import { useTranslation } from '@3nvi/gatsby-theme-intl';

const Component = () => {
  const { t } = useTranslation();

  return <div>{t('greeting')}</div>;
};

Link

A wrapper around gatsby-link, that accepts the original path and converts it to a intl-aware link, depending on the currently active language.

import { Link } from '@3nvi/gatsby-theme-intl';

const Component = () => {
  return <Link to="/about">About</Link>; // destination gets automatically converted to `/{activeLanguage}/about`
};

In addition to these, the package configures & forwards all React components present in the react-i18next package.

Quick Start

This plugin composes gatsby-plugin-intl:

npm i @3nvi/gatsby-theme-intl

and in your gatsby-config.js:

{
  // ... rest of your config
  plugins: [
    // ... other plugins
    {
      resolve: `@3nvi/gatsby-theme-intl`,
      options: {
        // ...
      },
    },
  ];
}

Configuration

The plugin accepts all optional options present in gatsby-plugin-intl. Additionally, it accepts the following:

  • i18nextConfig: Configuration options for the i18next instance that this theme uses under the hood. The available options can be found in the official docs.

    This package already adds a sane default configuration for i18next, which is automatically merged with the one you provide. The minimum required configuration option from your part is the resources option.

Example configuration:

const translations = require('./i18n.json');

{
  // ... rest of your config
  plugins: [
    // ... other plugins
    {
      resolve: `@3nvi/gatsby-theme-intl`,
      options: {
        supportedLanguages: ['en', 'fr']
        i18nextConfig: {
          resources: translations,
        },
      },
    },
  ];
}

Client-Only Routes

For implementing client-only routes gatsby recommends using its dedicated gatsby-plugin-create-client-paths. This can work in tandem with gatsby-theme-intl by simply:

  1. Including gatsby-plugin-create-client-paths before gastby-theme-intl:
const translations = require('./i18n.json');

{
  // ... rest of your config
  plugins: [
    // ... other plugins
    {
      resolve: `gatsby-plugin-create-client-paths`,
      options: { prefixes: [`/app/*`] },
    },
    {
      resolve: `@3nvi/gatsby-theme-intl`,
      options: {
        supportedLanguages: ['en', 'fr']
        i18nextConfig: {
          resources: translations,
        },
      },
    },
  ];
}
  1. Making sure that the Router component's basePath contains the language prefix before the dynamic URL part. You can get the current language from usePageContext. For example, using the previous config (where all /app/* paths were client-only), we would do:
// app.js
const App = () => {
  const { lang } = usePageContext();

  return (
    <Router basepath={`/${lang}/app`}>
      <ComponentOne path="/client-only-route-1" />
      <ComponentTwo path="/client-only-route-2" />
    </Router>
  );
};

Usage Examples

Visit the related gatsby starter to see a full example of how this plugin can be used

Changelog

Please refer to the Changelog for information on the details of each release

License

MIT