-
Notifications
You must be signed in to change notification settings - Fork 12
feat(use-i18n): add i18n hook #110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6263168
feat(use-i18n): add i18n hook
philibea 54f08bd
chore(deps-dev): bump @babel/preset-react from 7.12.13 to 7.13.13 (#99)
dependabot[bot] df6cc9b
chore(deps): bump @babel/eslint-parser from 7.13.10 to 7.13.14 (#108)
dependabot[bot] ce94d4f
feat(use-i18n): add i18n hook
philibea 27a33eb
Merge branch 'master' into use-i18n
philibea File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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__/** | ||
philibea marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| examples/ | ||
| src | ||
| !.npmignore | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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. | ||
philibea marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| # 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.