Package Exports
- @3nvi/gatsby-theme-intl
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@3nvi/gatsby-theme-intl) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
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.Contextto 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-intland 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 thei18nextinstance 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
resourcesoption.
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:
- Including
gatsby-plugin-create-client-pathsbeforegastby-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,
},
},
},
];
}- Making sure that the Router component's
basePathcontains the language prefix before the dynamic URL part. You can get the current language fromusePageContext. 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