|
| 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 | +``` |
0 commit comments