Skip to content

Commit 2716621

Browse files
authored
Merge pull request #166 from gdams/gatsby5
add support for Gatsby 5.x
2 parents f28152a + 79310cf commit 2716621

File tree

15 files changed

+3545
-6249
lines changed

15 files changed

+3545
-6249
lines changed

README.md

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ plugins: [
7272
localeJsonSourceName: `locale`, // name given to `gatsby-source-filesystem` plugin.
7373
languages: [`en`, `es`, `de`],
7474
defaultLanguage: `en`,
75-
// if you are using Helmet, you must include siteUrl, and make sure you add http:https
7675
siteUrl: `https://example.com`,
7776
// if you are using trailingSlash gatsby config include it here, as well (the default is 'always')
7877
trailingSlash: 'always',
@@ -108,8 +107,8 @@ This example is not using semantic keys instead the entire message will be used
108107

109108
For example,
110109

111-
| language resource files | language |
112-
| -------------------------------------------------------------------- | -------- |
110+
| language resource files | language |
111+
| ---------------------------------------------------------------------------------------------------------------------------- | -------- |
113112
| [/locales/en/index.json](https://github.com/microapps/gatsby-plugin-react-i18next/blob/master/example/locales/en/index.json) | English |
114113
| [/locales/es/index.json](https://github.com/microapps/gatsby-plugin-react-i18next/blob/master/example/locales/es/index.json) | Spanish |
115114
| [/locales/de/index.json](https://github.com/microapps/gatsby-plugin-react-i18next/blob/master/example/locales/de/index.json) | German |
@@ -140,7 +139,7 @@ Use react i18next [`useTranslation`](https://react.i18next.com/latest/usetransla
140139
Replace [Gatsby `Link`](https://www.gatsbyjs.org/docs/gatsby-link) component with the `Link` component exported from `gatsby-plugin-react-i18next`
141140

142141
```javascript
143-
import { graphql } from "gatsby"
142+
import {graphql} from 'gatsby';
144143
import React from 'react';
145144
import {Link, Trans, useTranslation} from 'gatsby-plugin-react-i18next';
146145
import Layout from '../components/layout';
@@ -319,12 +318,6 @@ const SpanishAboutLink = () => (
319318
);
320319
```
321320

322-
### `Helmet`
323-
324-
`Helmet` component is identical to [`gatsby-plugin-react-helmet`](https://www.gatsbyjs.org/packages/gatsby-plugin-react-helmet) component but also provides language related metatags (alternative and canonical links)
325-
326-
**Note!** To use it you need to have `react-helmet` dependency installed. You also need to provide `siteUrl` in plugin options for it to work properly.
327-
328321
### `I18nextContext`
329322

330323
Use this react context to access language information about the page
@@ -385,13 +378,13 @@ In this example the plugin will automatically generate language pages for all la
385378

386379
Omit `excludeLanguages` to get all the languages form the path. Make sure that you have pages for all the languages that you specify in the plugin, otherwise you might have broken links.
387380

388-
You may also need to add a pages config for the 404 page, if it uses the same path pattern as your excluded pages. Note that the order of these rules is important.
381+
You may also need to add a pages config for the 404 page, if it uses the same path pattern as your excluded pages. Note that the order of these rules is important.
389382

390383
```js
391384
pages: [
392385
{
393386
matchPath: '/:lang?/404',
394-
getLanguageFromPath: false,
387+
getLanguageFromPath: false
395388
},
396389
{
397390
matchPath: '/:lang?/:uid',
@@ -518,6 +511,7 @@ By default this plugin is setup to fallback on the entire **message string**, th
518511
In order to use **semantic keys**, so the fallback message string is the default's language value (instead of the key), it is possible to do the following;
519512

520513
In `/gatsby-config.js`, setup the plugin as usual, and add the key `options.i18nextOptions.fallbackLng` (i18next documentation, [configuration options](https://www.i18next.com/overview/configuration-options#languages-namespaces-resources), and [fallback options](https://www.i18next.com/principles/fallback#fallback));
514+
521515
```
522516
{
523517
resolve: `gatsby-plugin-react-i18next`,
@@ -663,8 +657,8 @@ module.exports = {
663657

664658
## Mentions
665659

666-
- [Best internationalization for Gatsby](https://dev.to/adrai/best-internationalization-for-gatsby-mkf) by
667-
Adriano Raiano
660+
- [Best internationalization for Gatsby](https://dev.to/adrai/best-internationalization-for-gatsby-mkf) by
661+
Adriano Raiano
668662

669663
## Credits
670664

example/gatsby-config.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ module.exports = {
88
siteUrl: 'https://kind-lichterman-5edcb4.netlify.app'
99
},
1010
plugins: [
11-
`gatsby-plugin-react-helmet`,
1211
`gatsby-plugin-image`,
1312
{
1413
resolve: `gatsby-source-filesystem`,
@@ -66,7 +65,7 @@ module.exports = {
6665
{
6766
resolve: 'gatsby-plugin-sitemap',
6867
options: {
69-
exclude: ['/**/404', '/**/404.html'],
68+
excludes: ['/**/404', '/**/404.html'],
7069
query: `
7170
{
7271
site {

example/package.json

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,29 @@
55
"version": "0.1.0",
66
"author": "Dmitriy Nevzorov <dmitriy.nevzorov@microapps.com>",
77
"dependencies": {
8-
"gatsby": "^4.20.0",
9-
"gatsby-plugin-image": "^2.20.0",
10-
"gatsby-plugin-manifest": "^4.20.0",
11-
"gatsby-plugin-offline": "^5.20.0",
12-
"gatsby-plugin-react-helmet": "^5.20.0",
13-
"gatsby-plugin-react-i18next": "^2.0.4",
14-
"gatsby-plugin-sharp": "^4.20.0",
15-
"gatsby-plugin-sitemap": "^5.20.0",
16-
"gatsby-source-filesystem": "^4.20.0",
17-
"gatsby-transformer-sharp": "^4.20.0",
18-
"i18next": "^21.9.0",
8+
"gatsby": "^5.2.0",
9+
"gatsby-plugin-image": "^3.2.0",
10+
"gatsby-plugin-manifest": "^5.2.0",
11+
"gatsby-plugin-offline": "^6.2.0",
12+
"gatsby-plugin-react-i18next": "^2.0.5",
13+
"gatsby-plugin-sharp": "^5.2.0",
14+
"gatsby-plugin-sitemap": "^6.2.0",
15+
"gatsby-source-filesystem": "^5.2.0",
16+
"gatsby-transformer-sharp": "^5.2.0",
17+
"i18next": "^22.0.6",
1918
"prop-types": "^15.8.1",
2019
"react": "^18.2.0",
2120
"react-dom": "^18.2.0",
22-
"react-helmet": "^6.1.0",
23-
"react-i18next": "^11.18.3"
21+
"react-i18next": "^12.0.0"
2422
},
2523
"devDependencies": {
26-
"@babel/cli": "^7.18.10",
27-
"@babel/core": "^7.18.10",
28-
"aws-sdk": "^2.1190.0",
29-
"babel-plugin-i18next-extract": "^0.9.0-rc",
30-
"babel-preset-gatsby": "^2.20.0",
24+
"@babel/cli": "^7.19.3",
25+
"@babel/core": "^7.20.5",
26+
"aws-sdk": "^2.1266.0",
27+
"babel-plugin-i18next-extract": "^0.9.0",
28+
"babel-preset-gatsby": "^3.2.0",
3129
"mkdirp": "^1.0.4",
32-
"prettier": "2.7.1"
30+
"prettier": "^2.8.0"
3331
},
3432
"keywords": [
3533
"gatsby"

example/src/components/seo.js

Lines changed: 15 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@
77

88
import React from 'react';
99
import PropTypes from 'prop-types';
10-
import {Helmet} from 'gatsby-plugin-react-i18next';
1110
import {useStaticQuery, graphql} from 'gatsby';
1211

13-
function SEO({description, meta, title}) {
12+
function Seo({description, title}) {
1413
const {site} = useStaticQuery(
1514
graphql`
1615
query {
@@ -28,57 +27,28 @@ function SEO({description, meta, title}) {
2827
const metaDescription = description || site.siteMetadata.description;
2928

3029
return (
31-
<Helmet
32-
title={title}
33-
titleTemplate={`%s | ${site.siteMetadata.title}`}
34-
meta={[
35-
{
36-
name: `description`,
37-
content: metaDescription
38-
},
39-
{
40-
property: `og:title`,
41-
content: title
42-
},
43-
{
44-
property: `og:description`,
45-
content: metaDescription
46-
},
47-
{
48-
property: `og:type`,
49-
content: `website`
50-
},
51-
{
52-
name: `twitter:card`,
53-
content: `summary`
54-
},
55-
{
56-
name: `twitter:creator`,
57-
content: site.siteMetadata.author
58-
},
59-
{
60-
name: `twitter:title`,
61-
content: title
62-
},
63-
{
64-
name: `twitter:description`,
65-
content: metaDescription
66-
}
67-
].concat(meta)}
68-
/>
30+
<>
31+
<title>{title}</title>
32+
<meta name="description" content={metaDescription} />
33+
<meta property="og:title" content={title} />
34+
<meta property="og:description" content={metaDescription} />
35+
<meta property="og:type" content="website" />
36+
<meta property="twitter:card" content="summary" />
37+
<meta property="twitter:creator" content={site.siteMetadata.author} />
38+
<meta property="twitter:title" content={title} />
39+
<meta property="twitter:description" content={metaDescription} />
40+
</>
6941
);
7042
}
7143

72-
SEO.defaultProps = {
73-
meta: [],
44+
Seo.defaultProps = {
7445
description: ``
7546
};
7647

77-
SEO.propTypes = {
48+
Seo.propTypes = {
7849
description: PropTypes.string,
7950
lang: PropTypes.string,
80-
meta: PropTypes.arrayOf(PropTypes.object),
8151
title: PropTypes.string.isRequired
8252
};
8353

84-
export default SEO;
54+
export default Seo;

example/src/pages/404.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@ import {graphql} from 'gatsby';
44
import React from 'react';
55
import {useTranslation, Trans} from 'gatsby-plugin-react-i18next';
66
import Layout from '../components/layout';
7-
import SEO from '../components/seo';
7+
import Seo from '../components/seo';
88

99
const NotFoundPage = () => {
10-
const {t} = useTranslation();
1110
return (
1211
<Layout>
13-
<SEO title={t('404: Not found')} />
1412
<h1>
1513
<Trans>NOT FOUND</Trans>
1614
</h1>
@@ -21,6 +19,11 @@ const NotFoundPage = () => {
2119
);
2220
};
2321

22+
export const Head = () => {
23+
const {t} = useTranslation();
24+
return <Seo title={t('404: Not found')} />;
25+
};
26+
2427
export default NotFoundPage;
2528

2629
export const query = graphql`

example/src/pages/ignored-page.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import React from 'react';
22
import Layout from '../components/layout';
3-
import SEO from '../components/seo';
3+
import Seo from '../components/seo';
44
import {Link} from 'gatsby-plugin-react-i18next';
55

66
const IgnoredPage = (props) => {
77
return (
88
<Layout>
9-
<SEO title="Ignored page" />
109
<h1>Ignored page</h1>
1110
<p>This page does not have language prefix</p>
1211
<Link to="/">Go back to the homepage</Link>
1312
</Layout>
1413
);
1514
};
1615

16+
export const Head = () => <Seo title="Ignored page" />;
17+
1718
export default IgnoredPage;

example/src/pages/index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@ import {Link, Trans, useTranslation} from 'gatsby-plugin-react-i18next';
55
import {graphql, Link as GatsbyLink} from 'gatsby';
66
import {StaticImage} from 'gatsby-plugin-image';
77
import Layout from '../components/layout';
8-
import SEO from '../components/seo';
8+
import Seo from '../components/seo';
99

1010
const IndexPage = () => {
11-
const {t} = useTranslation();
1211
return (
1312
<Layout>
14-
<SEO title={t('Home')} />
1513
<h1>
1614
<Trans>Hi people</Trans>
1715
</h1>
@@ -44,6 +42,11 @@ const IndexPage = () => {
4442
);
4543
};
4644

45+
export const Head = () => {
46+
const {t} = useTranslation();
47+
return <Seo title={t('Home')} />;
48+
};
49+
4750
export default IndexPage;
4851

4952
export const query = graphql`

example/src/pages/page-2.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@
33
import {graphql} from 'gatsby';
44
import React from 'react';
55
import Layout from '../components/layout';
6-
import SEO from '../components/seo';
6+
import Seo from '../components/seo';
77
import {Link, useTranslation, Trans} from 'gatsby-plugin-react-i18next';
88

99
const SecondPage = (props) => {
10-
const {t} = useTranslation();
1110
return (
1211
<Layout>
13-
<SEO title={t('Page two')} />
1412
<h1>
1513
<Trans>Page two</Trans>
1614
</h1>
@@ -24,6 +22,11 @@ const SecondPage = (props) => {
2422
);
2523
};
2624

25+
export const Head = () => {
26+
const {t} = useTranslation();
27+
return <Seo title={t('Page two')} />;
28+
};
29+
2730
export default SecondPage;
2831

2932
export const query = graphql`

0 commit comments

Comments
 (0)