Skip to content

Commit

Permalink
- improve README.md
Browse files Browse the repository at this point in the history
- add path to i18nextContext

Signed-off-by: Dmitriy Nevzorov <[email protected]>
  • Loading branch information
jimmyn committed May 28, 2020
1 parent b9ed045 commit 6740e9d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ Content of the context object
| routed | boolean | if `false` it means that the page is in default language |
| defaultLanguage | string | default language provided in plugin options |
| originalPath | string | page path in default language |
| path | string | page path |

The same context will be also available in the Gatsby `pageContext.i18n` object

Expand All @@ -250,7 +251,7 @@ You can use [babel-plugin-i18next-extract](https://i18next-extract.netlify.app)
1. Install

```
yarn add @babel/cli babel-plugin-i18next-extract -D
yarn add @babel/cli @babel/plugin-transform-typescript babel-plugin-i18next-extract -D
```

2. create `babel-extract.config.js` file (don't name it `babel.config.js`, or it will be used by gatsby)
Expand All @@ -271,6 +272,12 @@ module.exports = {
customTransComponents: [['gatsby-plugin-react-i18next', 'Trans']]
}
]
],
overrides: [
{
test: [`**/*.ts`, `**/*.tsx`],
plugins: [[`@babel/plugin-transform-typescript`, {isTSX: true}]]
}
]
};
```
Expand All @@ -280,7 +287,7 @@ module.exports = {
```json
{
"scripts": {
"extract": "yarn run babel -f babel-extract.config.js -o tmp/chunk.js 'src/**/*.{js,jsx,ts,tsx}' && rm -rf tmp"
"extract": "yarn run babel --config-file ./babel-extract.config.js -o tmp/chunk.js 'src/**/*.{js,jsx,ts,tsx}' && rm -rf tmp"
}
}
```
Expand Down
3 changes: 2 additions & 1 deletion src/i18nextContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ export const I18nextContext = React.createContext<I18NextContext>({
languages: ['en'],
routed: false,
defaultLanguage: 'en',
originalPath: '/'
originalPath: '/',
path: '/'
});
9 changes: 8 additions & 1 deletion src/plugin/wrapPageElement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,14 @@ export const wrapPageElement = (
i18n.changeLanguage(language);
}

const context = {routed, language, languages, originalPath, defaultLanguage};
const context = {
routed,
language,
languages,
originalPath,
defaultLanguage,
path: props.path
};

return withI18next(i18n, context)(element);
};
1 change: 1 addition & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export type I18NextContext = {
languages: string[];
defaultLanguage: string;
originalPath: string;
path: string;
};

export type PageContext = {
Expand Down

0 comments on commit 6740e9d

Please sign in to comment.