Skip to content

Commit a9f0004

Browse files
committed
feat(use-i18n): add i18n hook
1 parent df6cc9b commit a9f0004

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

+1704
-327
lines changed

.eslintignore

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

.eslintrc

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

packages/eslint-config-react/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,15 @@ module.exports = {
4545
'react/jsx-no-useless-fragment': 'error',
4646
'react/no-adjacent-inline-elements': 'error',
4747
'react/jsx-no-constructed-context-values': 'warn',
48+
'import/no-extraneous-dependencies': [
49+
'error',
50+
{
51+
devDependencies: [
52+
'**/*.test.js',
53+
'**/*.spec.js',
54+
'**/__tests__/*.js',
55+
],
56+
},
57+
],
4858
},
4959
}

packages/use-i18n/README.md

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,52 @@ $ yarn add @scaleway/use-i18n
99
```
1010

1111
## Usage
12+
### Loading locales
13+
Create a directory with your locales.
14+
Use of local variables and namespace to dynamically load the 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 defaultTranslations from './locales/en/common';
38+
39+
<I18N
40+
defaultLocale="en"
41+
supportedLocales={['en']}
42+
defaultTranslations={defaultTranslations}
43+
>
44+
<App />
45+
</I18N>
46+
```
47+
48+
1249

1350
```js
1451
import React from 'react'
1552
import I18n from '@scaleway/use-i18n'
1653

54+
1755
const Page = () => {
18-
const { t } = useI18n()
56+
// this will load locales based on `./locales/${currentLocale}/common.json`
57+
const { t } = useTranlation(['common'])
1958

2059
return <h1>{t('title')}</h1>
2160
}
@@ -26,10 +65,13 @@ const App = () => {
2665
title: 'Welcome to I18n hooks',
2766
}
2867

68+
const load = ({ locale, namespace }) =>
69+
import(`./locales/${locale}/${namespace}`)
70+
2971
return (
3072
<I18n
3173
defaultLocale="en"
32-
defaultLocales={defaultLocales}
74+
supportedLocales={defaultLocales}
3375
defaultTranslations={defaultTranslations}
3476
>
3577
<Page />
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# From .gitignore
2+
cra
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+
}

packages/use-i18n/example/cra/README.md

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

packages/use-i18n/example/cra/package.json

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
"version": "0.1.0",
44
"private": true,
55
"dependencies": {
6-
"@scaleway/use-i18n": "file:.yalc/@scaleway/use-i18n",
6+
"@emotion/react": "^11.1.5",
7+
"@emotion/styled": "^11.3.0",
8+
"@scaleway/eslint-config-react": "^1.4.0",
9+
"@scaleway/ui": "^0.75.3",
10+
"@scaleway/use-i18n": "file:../../",
711
"react": "^17.0.2",
812
"react-dom": "^17.0.2",
913
"react-scripts": "4.0.3",
@@ -14,12 +18,6 @@
1418
"build": "react-scripts build",
1519
"eject": "react-scripts eject"
1620
},
17-
"eslintConfig": {
18-
"extends": [
19-
"react-app",
20-
"react-app/jest"
21-
]
22-
},
2321
"browserslist": {
2422
"production": [
2523
">0.2%",
-3.78 KB
Binary file not shown.

0 commit comments

Comments
 (0)