Skip to content

Commit 1c27e01

Browse files
committed
- update example
- automatically configure fallback namespaces
1 parent 3785097 commit 1c27e01

38 files changed

+6406
-6392
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,6 @@ yarn-error.log
7171

7272
.idea
7373
dist/
74+
75+
**/.yalc
76+
**/yalc.lock

README.md

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Easily translate your Gatsby website into multiple languages.
55
## Features
66

77
- Seamless integration with [react-i18next](https://react.i18next.com/) - a powerful internationalization framework for React.
8-
- No extra graphql queries to fetch translations, everything is done automatically.
8+
- Code splitting. Load translations for each page separately.
99
- Automatic redirection based on the user's preferred language in browser provided by [browser-lang](https://github.com/wiziple/browser-lang).
1010
- Support multi-language url routes in a single page component. You don't have to create separate pages such as `pages/en/index.js` or `pages/es/index.js`.
1111
- SEO friendly
@@ -17,7 +17,7 @@ When you build multilingual sites, Google recommends using different URLs for ea
1717

1818
## :boom: Breaking change since v0.0.27
1919

20-
As of v0.0.28, language JSON resources should be loaded by `gatsby-source-filesystem` plugin and than fetched by GraphQL query. It enables incremental build and hot-reload as language JSON files change.
20+
As of v0.0.28, language JSON resources should be loaded by `gatsby-source-filesystem` plugin and then fetched by GraphQL query. It enables incremental build and hot-reload as language JSON files change.
2121

2222
Users who have loaded language JSON files using `path` option will be affected. Please check configuration example on below.
2323

@@ -55,8 +55,7 @@ plugins: [
5555
resolve: `gatsby-source-filesystem`,
5656
options: {
5757
path: `${__dirname}/locales`,
58-
name: `locale`,
59-
ignore: [`**/\.*`, `**/*~`]
58+
name: `locale`
6059
}
6160
},
6261
{
@@ -113,8 +112,8 @@ For example:
113112

114113
```
115114
|-- en
116-
|-- header.json
117-
|-- footer.json
115+
|-- common.json
116+
|-- index.json
118117
```
119118

120119
The default namespace is `translation`. [Read more about i18next namespaces](https://www.i18next.com/principles/namespaces)
@@ -382,35 +381,38 @@ pages: [
382381
];
383382
```
384383

385-
## How to fetch language specific data
384+
## How to fetch translations of specific namespaces only
386385

387-
You can use `language` variable in gatsby page queries to fetch additional data for each language. For example if you're using [gatsby-transformer-json](https://www.gatsbyjs.org/packages/gatsby-transformer-json/) your query might look like:
386+
You can use `ns` and `language` field in gatsby page queries to fetch specific namespaces that are being used in the page. This will be useful when you have several big pages with lots of translations.
388387

389388
```javascript
390389
export const query = graphql`
391390
query($language: String!) {
392-
dataJson(language: {eq: $language}) {
393-
...DataFragment
391+
locales: allLocale(filter: {ns: {in: ["common", "index"]}, language: {eq: $language}}) {
392+
edges {
393+
node {
394+
ns
395+
data
396+
language
397+
}
398+
}
394399
}
395400
}
396401
`;
397402
```
398403

399-
## How to fetch translations of specific namespaces only
404+
Note that in this case only files `common.json` and `index.json` will be loaded.
405+
This plugin will automatically add all loaded namespaces as fallback namespaces so if you don't specify a namespace in your translations they will still work.
400406

401-
You can use `ns` and `language` field in gatsby page queries to fetch specific namespaces that are being used in the page. This will be useful when you have several big pages with lots of translations.
407+
## How to fetch language specific data
408+
409+
You can use `language` variable in gatsby page queries to fetch additional data for each language. For example if you're using [gatsby-transformer-json](https://www.gatsbyjs.org/packages/gatsby-transformer-json/) your query might look like:
402410

403411
```javascript
404412
export const query = graphql`
405413
query($language: String!) {
406-
locales: allLocale(filter: {ns: {regex: "/common|about/"}, language: {eq: $language}}) {
407-
edges {
408-
node {
409-
ns
410-
data
411-
language
412-
}
413-
}
414+
dataJson(language: {eq: $language}) {
415+
...DataFragment
414416
}
415417
}
416418
`;
@@ -524,6 +526,34 @@ module.exports = {
524526
}
525527
```
526528

529+
If you want to extract translations per page, you can add a special comment at the beginning of the page:
530+
531+
```
532+
// i18next-extract-mark-ns-start about-page
533+
```
534+
535+
This will create a file `about-page.json` with all the translations on this page.
536+
537+
To load this file you need to specify a namespace like this:
538+
539+
```javascript
540+
export const query = graphql`
541+
query($language: String!) {
542+
locales: allLocale(
543+
filter: {ns: {in: ["translation", "about-page"]}, language: {eq: $language}}
544+
) {
545+
edges {
546+
node {
547+
ns
548+
data
549+
language
550+
}
551+
}
552+
}
553+
}
554+
`;
555+
```
556+
527557
### Automatically translate to different languages
528558

529559
After your messages had been extracted you can use [AWS Translate](https://aws.amazon.com/translate/) to automatically translate messages to different languages.

example/babel-extract.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ module.exports = {
1313
keyAsDefaultValue: [defaultLanguage],
1414
useI18nextDefaultValue: [defaultLanguage],
1515
discardOldKeys: true,
16+
defaultNS: 'common',
1617
outputPath: 'locales/{{locale}}/{{ns}}.json',
1718
customTransComponents: [['gatsby-plugin-react-i18next', 'Trans']]
1819
}

example/locales/de/404.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"404: Not found": "404: Nicht gefunden",
3+
"NOT FOUND": "NICHT GEFUNDEN",
4+
"You just hit a route that doesn't exist... the sadness.": "Du hast gerade eine Route angefahren, die es nicht gibt... die Traurigkeit."
5+
}

example/locales/de/common.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"Welcome to {{siteTitle}}": "Willkommen bei {{siteTitle}}"
3+
}

example/locales/de/index.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"Hi people": "Hallo Leute",
3+
"Home": "Nach Hause",
4+
"Welcome to your new Gatsby site.": "Willkommen auf deiner neuen Gatsby Seite.",
5+
"Go to ignored page": "Gehe zur ignorierten Seite",
6+
"Go to page 2": "Gehe zu Seite 2",
7+
"Now go build something great.": "Jetzt baue etwas Großartiges."
8+
}

example/locales/de/page-2.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"Page two": "Seite zwei",
3+
"Go back to the homepage": "Geh zurück zur Homepage",
4+
"Welcome to page 2": "Willkommen auf Seite 2"
5+
}

example/locales/de/translation.json

Lines changed: 0 additions & 14 deletions
This file was deleted.

example/locales/en/404.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"404: Not found": "404: Not found",
3+
"NOT FOUND": "NOT FOUND",
4+
"You just hit a route that doesn't exist... the sadness.": "You just hit a route that doesn't exist... the sadness."
5+
}

example/locales/en/common.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"Welcome to {{siteTitle}}": "Welcome to {{siteTitle}}"
3+
}

0 commit comments

Comments
 (0)