@3nvi/gatsby-plugin-intl 中文文档教程
gatsby-plugin-intl
通过为所有支持的语言环境创建页面来帮助国际化。 这是一个低级插件 gatsby-theme-intl 取决于。 你应该 仅当您已经有处理翻译的方法时才使用它。
Features
简而言之,这个插件:
- Creates a page for each of your locales and exposes them through different URLs
- 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
这个插件扫描你所有的页面(那些在 /pages
下的和那些动态创建的 通过 createPage
API)并根据您支持的语言环境为每个页面创建本地化版本。 在那里,它还会自动创建适当的 SEO 标记(取决于页面),因此您不必 必须担心手动实施 SEO
What it doesn't do
这个插件不处理翻译,而是将其委托给开发人员。 如果你想要一个简单的方法 在使用此插件时处理翻译,请访问 gatsby-theme-intl为 一体化解决方案。
Examples
假设您在 pages/about.js
下有一个页面,您支持的语言环境是 en
和 fr
(与 en
为默认值)。 该插件将创建以下内容:
- A french variation of about with proper SEO tags accessible under
/fr/about
- An english variation of about with proper SEO tags accessible under
/en/about
- Proper redirect definitions based on your
accept-language
header - Default redirect definitions from
/about
to/en/about/
whenaccept-language
is not present
如前所述,翻译委托给用户。 使用 gatsby-theme-intl 用于 如果你想要一个包含翻译的包。
Quick Start
此插件依赖于 react-helmet
作为对等依赖
npm i react-helmet gatsby-plugin-react-helmet @3nvi/gatsby-plugin-intl
项,并且在您的 gatsby-config.js
中:
{
// ... rest of your config
plugins: [
// ... other plugins
`gatsby-plugin-react-helmet`,
`@3nvi/gatsby-plugin-intl`
]
}
Configuration
该插件接受以下可选选项:
supportedLanguages
: An array of locales that your application supports. Defaults to['en']
.defaultLanguage
: The default/fallback locale of your application. Defaults toen
.siteUrl
: The URL of your site that's used when creating SEO tags. Defaults to the environment variableURL
(which platforms like Netlify automatically populate) or tolocalhost:8000
if the environment variable is not present.excludedPages
: A list page paths that the plugin should ignore. Defaults to[]
.notFoundPage
: The URL for a custom 404 page. Defaults to/404/
.deleteOriginalPages
: A boolean denoting whether to delete the original non-localized pages or retain them at their original paths. Defaults totrue
.
Client-Only Routes
请参考gatsby-theme-intl中的相关部分
Changelog
请参考Changelog 了解每个版本的详细信息
License
麻省理工学院
gatsby-plugin-intl
Helps with i18n by creating pages for all your supported locales. This is a low level plugin that gatsby-theme-intl depends upon. You should be using it only if you already have a methodology for handling translations.
Features
In short, this plugin:
- Creates a page for each of your locales and exposes them through different URLs
- 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
This plugin scans all your pages (those under /pages
and the ones created dynamically through the createPage
API) and creates localized versions for each one, depending on your supported locales. While there, it also automatically creates the proper SEO tags (depending on the page), so you don't have to worry about implementing the SEO manually
What it doesn't do
This plugin doesn't handle translations, but delegates that to the developer. If you want an easy way to handle translations while using this plugin, please visit gatsby-theme-intl for an all-in-one solution.
Examples
Let's say you have a single page under pages/about.js
and your supported locales are en
and fr
(with en
being the default one). This plugin will create the following:
- A french variation of about with proper SEO tags accessible under
/fr/about
- An english variation of about with proper SEO tags accessible under
/en/about
- Proper redirect definitions based on your
accept-language
header - Default redirect definitions from
/about
to/en/about/
whenaccept-language
is not present
As stated, translation is delegated to the user. Use gatsby-theme-intl for if you want a package that includes translations.
Quick Start
This plugin depends on react-helmet
as a peer dependency
npm i react-helmet gatsby-plugin-react-helmet @3nvi/gatsby-plugin-intl
and in your gatsby-config.js
:
{
// ... rest of your config
plugins: [
// ... other plugins
`gatsby-plugin-react-helmet`,
`@3nvi/gatsby-plugin-intl`
]
}
Configuration
The plugin accepts the following optional options:
supportedLanguages
: An array of locales that your application supports. Defaults to['en']
.defaultLanguage
: The default/fallback locale of your application. Defaults toen
.siteUrl
: The URL of your site that's used when creating SEO tags. Defaults to the environment variableURL
(which platforms like Netlify automatically populate) or tolocalhost:8000
if the environment variable is not present.excludedPages
: A list page paths that the plugin should ignore. Defaults to[]
.notFoundPage
: The URL for a custom 404 page. Defaults to/404/
.deleteOriginalPages
: A boolean denoting whether to delete the original non-localized pages or retain them at their original paths. Defaults totrue
.
Client-Only Routes
Please refer to the related section in gatsby-theme-intl
Changelog
Please refer to the Changelog for information on the details of each release
License
MIT