Skip to content

Commit

Permalink
feat(use-i18n): add i18n hook (scaleway#110)
Browse files Browse the repository at this point in the history
* 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
philibea and dependabot[bot] authored Apr 14, 2021
1 parent a9a3ca9 commit 27b3e5f
Show file tree
Hide file tree
Showing 49 changed files with 13,764 additions and 21 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# From .gitignore
node_modules/
dist/
build/
examples/
11 changes: 10 additions & 1 deletion .eslintrc
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"]
}
]
}
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ coverage
*.log
.reports
.env
build
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ scaleway-lib is a set of NPM packages used at Scaleway.
![npm bundle size](https://img.shields.io/bundlephobia/min/@scaleway/use-query-params)
![npm](https://img.shields.io/npm/v/@scaleway/use-query-params)

- [`@scaleway/use-i18n`](./packages/use-i18n/README.md):
A tiny hook to handle i18n.

![npm](https://img.shields.io/npm/dm/@scaleway/use-i18n)
![npm bundle size](https://img.shields.io/bundlephobia/min/@scaleway/use-i18n)
![npm](https://img.shields.io/npm/v/@scaleway/use-i18n)

- [`@scaleway/regex`](./packages/regex/README.md): usefull regex named.

![npm](https://img.shields.io/npm/dm/@scaleway/regex)
Expand Down
2 changes: 1 addition & 1 deletion babel.config.json
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"]
}
13 changes: 12 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,25 @@
"type": "module",
"devDependencies": {
"@babel/core": "^7.12.10",
"@babel/eslint-parser": "^7.13.10",
"@babel/plugin-transform-runtime": "^7.12.10",
"@babel/preset-env": "^7.12.11",
"@babel/preset-react": "7.13.13",
"@babel/preset-react": "^7.13.13",
"@commitlint/cli": "^12.0.0",
"@commitlint/config-conventional": "^12.0.0",
"@rollup/plugin-babel": "^5.2.2",
"@rollup/plugin-commonjs": "^18.0.0",
"@rollup/plugin-node-resolve": "^11.0.1",
"@testing-library/jest-dom": "^5.11.9",
"@testing-library/react": "^11.2.5",
"@testing-library/react-hooks": "^5.1.0",
"builtin-modules": "^3.2.0",
"cross-env": "^7.0.3",
"eslint": "^7.18.0",
"husky": "^6.0.0",
"jest": "^26.6.3",
"jest-junit": "^12.0.0",
"jest-localstorage-mock": "^2.4.8",
"lerna": "4.0.0",
"lint-staged": "^10.5.2",
"prettier": "^2.2.1",
Expand Down Expand Up @@ -57,9 +62,15 @@
}
},
"jest": {
"setupFiles": [
"jest-localstorage-mock"
],
"collectCoverageFrom": [
"packages/*/src/*.{js,jsx}"
],
"modulePathIgnorePatterns": [
"locales"
],
"coverageReporters": [
"text",
"cobertura"
Expand Down
4 changes: 4 additions & 0 deletions packages/use-i18n/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
**/__tests__/**
examples/
src
!.npmignore
141 changes: 141 additions & 0 deletions packages/use-i18n/README.md
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')
```
4 changes: 4 additions & 0 deletions packages/use-i18n/examples/cra/.babelrc
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"]
}
5 changes: 5 additions & 0 deletions packages/use-i18n/examples/cra/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"root": true,
"parser": "@babel/eslint-parser",
"extends": ["@scaleway/react"]
}
24 changes: 24 additions & 0 deletions packages/use-i18n/examples/cra/.gitignore
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
33 changes: 33 additions & 0 deletions packages/use-i18n/examples/cra/package.json
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.
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 not shown.
53 changes: 53 additions & 0 deletions packages/use-i18n/examples/cra/public/index.html
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>
25 changes: 25 additions & 0 deletions packages/use-i18n/examples/cra/public/manifest.json
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"
}
Loading

0 comments on commit 27b3e5f

Please sign in to comment.