Skip to content

Commit 27b3e5f

Browse files
feat(use-i18n): add i18n hook (#110)
* feat(use-i18n): add i18n hook * chore(deps-dev): bump @babel/preset-react from 7.12.13 to 7.13.13 (#99) Bumps [@babel/preset-react](https://github.com/babel/babel/tree/HEAD/packages/babel-preset-react) from 7.12.13 to 7.13.13. - [Release notes](https://github.com/babel/babel/releases) - [Changelog](https://github.com/babel/babel/blob/main/CHANGELOG.md) - [Commits](https://github.com/babel/babel/commits/v7.13.13/packages/babel-preset-react) Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * chore(deps): bump @babel/eslint-parser from 7.13.10 to 7.13.14 (#108) Bumps [@babel/eslint-parser](https://github.com/babel/babel/tree/HEAD/eslint/babel-eslint-parser) from 7.13.10 to 7.13.14. - [Release notes](https://github.com/babel/babel/releases) - [Changelog](https://github.com/babel/babel/blob/main/CHANGELOG.md) - [Commits](https://github.com/babel/babel/commits/v7.13.14/eslint/babel-eslint-parser) Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * feat(use-i18n): add i18n hook Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
1 parent a9a3ca9 commit 27b3e5f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+13764
-21
lines changed

.eslintignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
# From .gitignore
22
node_modules/
33
dist/
4+
build/
5+
examples/

.eslintrc

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11
{
22
"root": true,
3-
"extends": "./packages/eslint-config-react/index.js"
3+
"parser": "@babel/eslint-parser",
4+
"extends": "./packages/eslint-config-react/index.js",
5+
"rules": {
6+
"import/no-extraneous-dependencies": [
7+
"error",
8+
{
9+
"devDependencies": ["**/__tests__/*", "rollup.config.mjs"]
10+
}
11+
]
12+
}
413
}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ coverage
55
*.log
66
.reports
77
.env
8+
build

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ scaleway-lib is a set of NPM packages used at Scaleway.
5858
![npm bundle size](https://img.shields.io/bundlephobia/min/@scaleway/use-query-params)
5959
![npm](https://img.shields.io/npm/v/@scaleway/use-query-params)
6060

61+
- [`@scaleway/use-i18n`](./packages/use-i18n/README.md):
62+
A tiny hook to handle i18n.
63+
64+
![npm](https://img.shields.io/npm/dm/@scaleway/use-i18n)
65+
![npm bundle size](https://img.shields.io/bundlephobia/min/@scaleway/use-i18n)
66+
![npm](https://img.shields.io/npm/v/@scaleway/use-i18n)
67+
6168
- [`@scaleway/regex`](./packages/regex/README.md): usefull regex named.
6269

6370
![npm](https://img.shields.io/npm/dm/@scaleway/regex)

babel.config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"presets": ["@babel/preset-env","@babel/preset-react"],
2+
"presets": ["@babel/preset-env", "@babel/preset-react"],
33
"plugins": ["@babel/plugin-transform-runtime"]
44
}

package.json

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,25 @@
77
"type": "module",
88
"devDependencies": {
99
"@babel/core": "^7.12.10",
10+
"@babel/eslint-parser": "^7.13.10",
1011
"@babel/plugin-transform-runtime": "^7.12.10",
1112
"@babel/preset-env": "^7.12.11",
12-
"@babel/preset-react": "7.13.13",
13+
"@babel/preset-react": "^7.13.13",
1314
"@commitlint/cli": "^12.0.0",
1415
"@commitlint/config-conventional": "^12.0.0",
1516
"@rollup/plugin-babel": "^5.2.2",
1617
"@rollup/plugin-commonjs": "^18.0.0",
1718
"@rollup/plugin-node-resolve": "^11.0.1",
19+
"@testing-library/jest-dom": "^5.11.9",
20+
"@testing-library/react": "^11.2.5",
21+
"@testing-library/react-hooks": "^5.1.0",
1822
"builtin-modules": "^3.2.0",
1923
"cross-env": "^7.0.3",
2024
"eslint": "^7.18.0",
2125
"husky": "^6.0.0",
2226
"jest": "^26.6.3",
2327
"jest-junit": "^12.0.0",
28+
"jest-localstorage-mock": "^2.4.8",
2429
"lerna": "4.0.0",
2530
"lint-staged": "^10.5.2",
2631
"prettier": "^2.2.1",
@@ -57,9 +62,15 @@
5762
}
5863
},
5964
"jest": {
65+
"setupFiles": [
66+
"jest-localstorage-mock"
67+
],
6068
"collectCoverageFrom": [
6169
"packages/*/src/*.{js,jsx}"
6270
],
71+
"modulePathIgnorePatterns": [
72+
"locales"
73+
],
6374
"coverageReporters": [
6475
"text",
6576
"cobertura"

packages/use-i18n/.npmignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
**/__tests__/**
2+
examples/
3+
src
4+
!.npmignore

packages/use-i18n/README.md

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# `@scaleway/use-i18n`
2+
3+
## A tiny hooks to handle i18n translation
4+
5+
## Install
6+
7+
```bash
8+
$ yarn add @scaleway/use-i18n
9+
```
10+
11+
## Usage
12+
### Loading locales
13+
Create a directory with your locales.
14+
Use of local `variables` and `namespace` to dynamically load locales.
15+
16+
**Exemple :**
17+
18+
```
19+
📦locales
20+
┣ 📂de
21+
┃ ┗ 📜common.json
22+
┣ 📂en
23+
┃ ┗ 📜common.json
24+
┗ 📂fr
25+
┃ ┗ 📜common.json
26+
```
27+
your loader will be:
28+
```js
29+
const load = ({ locale, namespace }) =>
30+
import(`./locales/${locale}/${namespace}`)
31+
```
32+
Inside your app you will need to use useTranslation to load namespace locales.
33+
34+
if you want to have pre-load locales you can use defaultTranslations.
35+
36+
```js
37+
import I18N from '@scaleway/use-i18n'
38+
import defaultTranslations from './locales/en/common';
39+
40+
<I18N
41+
defaultLocale="en"
42+
supportedLocales={['en']}
43+
defaultTranslations={defaultTranslations}
44+
>
45+
<App />
46+
</I18N>
47+
```
48+
49+
50+
51+
```js
52+
import React from 'react'
53+
import I18n from '@scaleway/use-i18n'
54+
55+
56+
const Page = () => {
57+
// this will load locales based on `./locales/${currentLocale}/common.json`
58+
const { t } = useTranlation(['common'])
59+
60+
return <h1>{t('title')}</h1>
61+
}
62+
63+
const App = () => {
64+
const defaultLocales = ['fr', 'en']
65+
const defaultTranslations = {
66+
title: 'Welcome to I18n hooks',
67+
}
68+
69+
const load = ({ locale, namespace }) =>
70+
import(`./locales/${locale}/${namespace}`)
71+
72+
return (
73+
<I18n
74+
defaultLocale="en"
75+
supportedLocales={defaultLocales}
76+
defaultTranslations={defaultTranslations}
77+
>
78+
<Page />
79+
</I18n>
80+
)
81+
}
82+
```
83+
84+
### useTranslation & useI18n
85+
86+
Theses both hooks are using the same context.
87+
useTranslation will load your locales with a use effect.
88+
Dynamique locale need to be loaded before using useI18n on an other file.
89+
90+
```js
91+
import { useTranslation } from '@scaleway/use-i18n'
92+
93+
const App = () => {
94+
const i18n = useTranslation(['app','common'])
95+
96+
return <>{i18n.t('app.user')}(</>
97+
}
98+
99+
```
100+
101+
```js
102+
import { useI18n } from '@scaleway/use-i18n'
103+
104+
const App = () => {
105+
const i18n = useTranslation()
106+
const { loadTranslations } = i18n
107+
const namespaces = ['app','common']
108+
109+
const key = namespaces.join(',')
110+
111+
useEffect(
112+
() =>
113+
key.split(',').map(async namespace => loadTranslations(namespace, load)),
114+
[loadTranslations, key, load],
115+
)
116+
117+
return <>{i18n.t('app.user')}(</>
118+
}
119+
120+
```
121+
122+
123+
124+
```js
125+
import {useI18n} from '@scaleway/use-i18n'
126+
127+
const { namespaceTranslation} = useI18n()
128+
const t = namespaceTranslation('namespace.home.users.table.header')
129+
```
130+
131+
### use namespaceTranslation
132+
133+
Namespace translation help you when you have some very long key
134+
Exemple of your locale key: `namespace.home.users.table.header.link`
135+
136+
```js
137+
import {useI18n} from '@scaleway/use-i18n'
138+
139+
const { namespaceTranslation} = useI18n()
140+
const t = namespaceTranslation('namespace.home.users.table.header')
141+
```
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"presets": ["@babel/preset-env", "@babel/preset-react"],
3+
"plugins": ["@babel/plugin-transform-runtime"]
4+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"root": true,
3+
"parser": "@babel/eslint-parser",
4+
"extends": ["@scaleway/react"]
5+
}

0 commit comments

Comments
 (0)