forked from scaleway/scaleway-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(use-i18n): add i18n hook (scaleway#110)
* feat(use-i18n): add i18n hook * chore(deps-dev): bump @babel/preset-react from 7.12.13 to 7.13.13 (scaleway#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] <support@github.com> 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 (scaleway#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] <support@github.com> 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>
- Loading branch information
1 parent
a9a3ca9
commit 27b3e5f
Showing
49 changed files
with
13,764 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
# From .gitignore | ||
node_modules/ | ||
dist/ | ||
build/ | ||
examples/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,13 @@ | ||
{ | ||
"root": true, | ||
"extends": "./packages/eslint-config-react/index.js" | ||
"parser": "@babel/eslint-parser", | ||
"extends": "./packages/eslint-config-react/index.js", | ||
"rules": { | ||
"import/no-extraneous-dependencies": [ | ||
"error", | ||
{ | ||
"devDependencies": ["**/__tests__/*", "rollup.config.mjs"] | ||
} | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ coverage | |
*.log | ||
.reports | ||
.env | ||
build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
{ | ||
"presets": ["@babel/preset-env","@babel/preset-react"], | ||
"presets": ["@babel/preset-env", "@babel/preset-react"], | ||
"plugins": ["@babel/plugin-transform-runtime"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
**/__tests__/** | ||
examples/ | ||
src | ||
!.npmignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
# `@scaleway/use-i18n` | ||
|
||
## A tiny hooks to handle i18n translation | ||
|
||
## Install | ||
|
||
```bash | ||
$ yarn add @scaleway/use-i18n | ||
``` | ||
|
||
## Usage | ||
### Loading locales | ||
Create a directory with your locales. | ||
Use of local `variables` and `namespace` to dynamically load locales. | ||
|
||
**Exemple :** | ||
|
||
``` | ||
📦locales | ||
┣ 📂de | ||
┃ ┗ 📜common.json | ||
┣ 📂en | ||
┃ ┗ 📜common.json | ||
┗ 📂fr | ||
┃ ┗ 📜common.json | ||
``` | ||
your loader will be: | ||
```js | ||
const load = ({ locale, namespace }) => | ||
import(`./locales/${locale}/${namespace}`) | ||
``` | ||
Inside your app you will need to use useTranslation to load namespace locales. | ||
|
||
if you want to have pre-load locales you can use defaultTranslations. | ||
|
||
```js | ||
import I18N from '@scaleway/use-i18n' | ||
import defaultTranslations from './locales/en/common'; | ||
|
||
<I18N | ||
defaultLocale="en" | ||
supportedLocales={['en']} | ||
defaultTranslations={defaultTranslations} | ||
> | ||
<App /> | ||
</I18N> | ||
``` | ||
|
||
|
||
|
||
```js | ||
import React from 'react' | ||
import I18n from '@scaleway/use-i18n' | ||
|
||
|
||
const Page = () => { | ||
// this will load locales based on `./locales/${currentLocale}/common.json` | ||
const { t } = useTranlation(['common']) | ||
|
||
return <h1>{t('title')}</h1> | ||
} | ||
|
||
const App = () => { | ||
const defaultLocales = ['fr', 'en'] | ||
const defaultTranslations = { | ||
title: 'Welcome to I18n hooks', | ||
} | ||
|
||
const load = ({ locale, namespace }) => | ||
import(`./locales/${locale}/${namespace}`) | ||
|
||
return ( | ||
<I18n | ||
defaultLocale="en" | ||
supportedLocales={defaultLocales} | ||
defaultTranslations={defaultTranslations} | ||
> | ||
<Page /> | ||
</I18n> | ||
) | ||
} | ||
``` | ||
|
||
### useTranslation & useI18n | ||
|
||
Theses both hooks are using the same context. | ||
useTranslation will load your locales with a use effect. | ||
Dynamique locale need to be loaded before using useI18n on an other file. | ||
|
||
```js | ||
import { useTranslation } from '@scaleway/use-i18n' | ||
|
||
const App = () => { | ||
const i18n = useTranslation(['app','common']) | ||
|
||
return <>{i18n.t('app.user')}(</> | ||
} | ||
|
||
``` | ||
```js | ||
import { useI18n } from '@scaleway/use-i18n' | ||
|
||
const App = () => { | ||
const i18n = useTranslation() | ||
const { loadTranslations } = i18n | ||
const namespaces = ['app','common'] | ||
|
||
const key = namespaces.join(',') | ||
|
||
useEffect( | ||
() => | ||
key.split(',').map(async namespace => loadTranslations(namespace, load)), | ||
[loadTranslations, key, load], | ||
) | ||
|
||
return <>{i18n.t('app.user')}(</> | ||
} | ||
|
||
``` | ||
```js | ||
import {useI18n} from '@scaleway/use-i18n' | ||
|
||
const { namespaceTranslation} = useI18n() | ||
const t = namespaceTranslation('namespace.home.users.table.header') | ||
``` | ||
### use namespaceTranslation | ||
Namespace translation help you when you have some very long key | ||
Exemple of your locale key: `namespace.home.users.table.header.link` | ||
```js | ||
import {useI18n} from '@scaleway/use-i18n' | ||
|
||
const { namespaceTranslation} = useI18n() | ||
const t = namespaceTranslation('namespace.home.users.table.header') | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"presets": ["@babel/preset-env", "@babel/preset-react"], | ||
"plugins": ["@babel/plugin-transform-runtime"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"root": true, | ||
"parser": "@babel/eslint-parser", | ||
"extends": ["@scaleway/react"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. | ||
|
||
# dependencies | ||
/node_modules | ||
/.pnp | ||
.pnp.js | ||
|
||
# testing | ||
/coverage | ||
|
||
# production | ||
/build | ||
|
||
# misc | ||
.DS_Store | ||
.env.local | ||
.env.development.local | ||
.env.test.local | ||
.env.production.local | ||
|
||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
.yalc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"name": "cra", | ||
"version": "0.1.0", | ||
"private": true, | ||
"dependencies": { | ||
"@emotion/react": "^11.1.5", | ||
"@emotion/styled": "^11.3.0", | ||
"@scaleway/eslint-config-react": "^1.4.0", | ||
"@scaleway/ui": "^0.75.3", | ||
"@scaleway/use-i18n": "file:../../", | ||
"react": "^17.0.2", | ||
"react-dom": "^17.0.2", | ||
"react-scripts": "4.0.3", | ||
"web-vitals": "^1.0.1" | ||
}, | ||
"scripts": { | ||
"start": "react-scripts start", | ||
"build": "react-scripts build", | ||
"eject": "react-scripts eject" | ||
}, | ||
"browserslist": { | ||
"production": [ | ||
">0.2%", | ||
"not dead", | ||
"not op_mini all" | ||
], | ||
"development": [ | ||
"last 1 chrome version", | ||
"last 1 firefox version", | ||
"last 1 safari version" | ||
] | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+2.48 KB
packages/use-i18n/examples/cra/public/favicon/apple-touch-icon-180x180.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions
6
packages/use-i18n/examples/cra/public/favicon/favicon-lightdark.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
<meta name="theme-color" content="#000000" /> | ||
<meta | ||
name="description" | ||
content="Web site created using create-react-app" | ||
/> | ||
<link | ||
rel="icon" | ||
type="image/svg+xml" | ||
href="%PUBLIC_URL%/favicon/favicon-lightdark.svg" | ||
/> | ||
<link rel="icon" href="%PUBLIC_URL%/favicon/favicon.ico" /> | ||
<link | ||
rel="apple-touch-icon" | ||
sizes="180x180" | ||
href="%PUBLIC_URL%/favicon/apple-touch-icon-180x180.png" | ||
/> | ||
<!-- | ||
manifest.json provides metadata used when your web app is installed on a | ||
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/ | ||
--> | ||
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" /> | ||
<!-- | ||
Notice the use of %PUBLIC_URL% in the tags above. | ||
It will be replaced with the URL of the `public` folder during the build. | ||
Only files inside the `public` folder can be referenced from the HTML. | ||
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will | ||
work correctly both with client-side routing and a non-root public URL. | ||
Learn how to configure a non-root public URL by running `npm run build`. | ||
--> | ||
<title>Scaleway I18n Hook</title> | ||
</head> | ||
<body> | ||
<noscript>You need to enable JavaScript to run this app.</noscript> | ||
<div id="root"></div> | ||
<!-- | ||
This HTML file is a template. | ||
If you open it directly in the browser, you will see an empty page. | ||
You can add webfonts, meta tags, or analytics to this file. | ||
The build step will place the bundled scripts into the <body> tag. | ||
To begin the development, run `npm start` or `yarn start`. | ||
To create a production bundle, use `npm run build` or `yarn build`. | ||
--> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"short_name": "React App", | ||
"name": "Create React App Sample", | ||
"icons": [ | ||
{ | ||
"src": "favicon.ico", | ||
"sizes": "64x64 32x32 24x24 16x16", | ||
"type": "image/x-icon" | ||
}, | ||
{ | ||
"src": "logo192.png", | ||
"type": "image/png", | ||
"sizes": "192x192" | ||
}, | ||
{ | ||
"src": "logo512.png", | ||
"type": "image/png", | ||
"sizes": "512x512" | ||
} | ||
], | ||
"start_url": ".", | ||
"display": "standalone", | ||
"theme_color": "#000000", | ||
"background_color": "#ffffff" | ||
} |
Oops, something went wrong.