Skip to content

Commit fdb675f

Browse files
authored
Merge pull request #77 from wiziple/versions/0.3.0
Versions/0.3.0
2 parents 0ba3b76 + 44cf72c commit fdb675f

File tree

14 files changed

+10984
-4675
lines changed

14 files changed

+10984
-4675
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,28 @@ const IndexPage = ({ intl }) => {
8989
}
9090
export default injectIntl(IndexPage)
9191
```
92+
Or you can use the new `useIntl` hook.
93+
```jsx
94+
import React from "react"
95+
import { useIntl, Link, FormattedMessage } from "gatsby-plugin-intl"
96+
97+
const IndexPage = () => {
98+
const intl = useIntl()
99+
return (
100+
<Layout>
101+
<SEO
102+
title={intl.formatMessage({ id: "title" })}
103+
/>
104+
<Link to="/page-2/">
105+
{intl.formatMessage({ id: "go_page2" })}
106+
{/* OR <FormattedMessage id="go_page2" /> */}
107+
</Link>
108+
</Layout>
109+
)
110+
}
111+
export default IndexPage
112+
```
113+
92114

93115
## How It Works
94116

__mocks__/webpack.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = {}

__tests__/fixtures/intl/es.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"spanish": "Spanish version"
3+
}

__tests__/gatsby-node.js

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const fs = require(`fs`)
2+
13
const { onCreatePage } = require(`../src/gatsby-node`)
24

35
const actions = {
@@ -9,10 +11,54 @@ const mocks = {
911
actions,
1012
page: {
1113
path: "/",
14+
context: {},
1215
},
1316
}
1417

15-
it(`does not crash when no pluginConfig is provided`, () => {
18+
beforeAll(() => {
19+
// Create file src/en.json because the default option for reading a file is `./en.json`
20+
fs.writeFileSync(
21+
`${__dirname}/../src/en.json`,
22+
JSON.stringify({ english: `English version` })
23+
)
24+
})
25+
26+
afterAll(() => {
27+
fs.unlinkSync(`${__dirname}/../src/en.json`)
28+
})
29+
30+
it(`should not crash when no pluginConfig is provided`, async () => {
1631
const pluginOptions = {}
17-
onCreatePage(mocks, pluginOptions)
32+
33+
await onCreatePage(mocks, pluginOptions)
34+
})
35+
36+
it(`should read translations from file and create corresponding pages`, async () => {
37+
const pluginOptions = {
38+
languages: [`es`],
39+
defaultLanguage: `es`,
40+
path: `${__dirname}/fixtures/intl`,
41+
}
42+
43+
await onCreatePage(mocks, pluginOptions)
44+
45+
expect(actions.createPage.mock.calls.length).toBe(4)
46+
47+
// assert the pages created match the requested languages
48+
expect(actions.createPage.mock.calls[0][0].path).toBe(`/`)
49+
expect(actions.createPage.mock.calls[1][0].path).toBe(`/en/`)
50+
expect(actions.createPage.mock.calls[2][0].path).toBe(`/`)
51+
expect(actions.createPage.mock.calls[3][0].path).toBe(`/es/`)
52+
})
53+
54+
it(`should crash when translations file doesn't exist`, async () => {
55+
const pluginOptions = {
56+
languages: [`es`, `en`],
57+
defaultLanguage: `es`,
58+
path: `${__dirname}/fixtures/intl`,
59+
}
60+
61+
await expect(onCreatePage(mocks, pluginOptions)).rejects.toThrow(
62+
`Cannot find module`
63+
)
1864
})

0 commit comments

Comments
 (0)