|
| 1 | +# Translate |
| 2 | + |
| 3 | +Provides translation API using [context](./context.md) interfaces. |
| 4 | + |
| 5 | + |
| 6 | +## `<Translations>` |
| 7 | + |
| 8 | +Translation provider, use it to broadcast your translation map. You can have multiple nested |
| 9 | +translations providers, where child provider will overwrite parent's translations. Also, you |
| 10 | +can have different translation providers at the same time, using the namespace `ns` prop. |
| 11 | + |
| 12 | +### Usage |
| 13 | + |
| 14 | +```jsx |
| 15 | +import {Translations} from 'libreact/lib/translate'; |
| 16 | + |
| 17 | +<Translations map={{ |
| 18 | + key: 'value', |
| 19 | + bye: 'ciao' |
| 20 | +}}> |
| 21 | + <App /> |
| 22 | +</Translations> |
| 23 | +``` |
| 24 | + |
| 25 | +### Props |
| 26 | + |
| 27 | +```ts |
| 28 | +interface ITranslationsProps { |
| 29 | + map: {[key: string]: string | ((T, ...args) => string)}; |
| 30 | + ns?: string; |
| 31 | +} |
| 32 | +``` |
| 33 | + |
| 34 | +, where |
| 35 | + |
| 36 | + - `map` - required, object, a dictionary of translations. |
| 37 | + - `ns` - optional, string, namespace. |
| 38 | + |
| 39 | + |
| 40 | +## `<Translate>` or `<T>` |
| 41 | + |
| 42 | +Translation consumer render prop component that receives translation function `T`, which has the following signature |
| 43 | + |
| 44 | +```ts |
| 45 | +T: (key: string, ...args) => string; |
| 46 | +``` |
| 47 | + |
| 48 | +`<Translate>` component has also a shorthand alias `<T>` component. |
| 49 | + |
| 50 | +### Usage |
| 51 | + |
| 52 | +```jsx |
| 53 | +import {Translations, T} from 'libreact/lib/translate'; |
| 54 | + |
| 55 | +<Translations map={{ |
| 56 | + key: 'value', |
| 57 | + bye: 'ciao' |
| 58 | +}}> |
| 59 | + <T>{(t) => |
| 60 | + <span>Goodbye is ${t('bye')}</span> |
| 61 | + }</T> |
| 62 | +</Translations> |
| 63 | +``` |
| 64 | + |
| 65 | +### Props |
| 66 | + |
| 67 | +```ts |
| 68 | +interface ITranslateProps { |
| 69 | + ns?: string; |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +, where |
| 74 | + |
| 75 | + - `ns` - optional, string, namespace. |
| 76 | + |
| 77 | + |
| 78 | +## `withT()` HOC |
| 79 | + |
| 80 | +`withT` is an enhancer that injects `T` function into your component. |
| 81 | + |
| 82 | +```jsx |
| 83 | +import {withT} from 'libreact/lib/translate'; |
| 84 | + |
| 85 | +const MyCompWithT = withT(MyComp); |
| 86 | +``` |
| 87 | + |
| 88 | + |
| 89 | +## `@withT` decorator |
| 90 | + |
| 91 | +Component class decorator that injects `T` function into your component. |
| 92 | + |
| 93 | +```jsx |
| 94 | +import {withT} from 'libreact/lib/translate'; |
| 95 | + |
| 96 | +@withT |
| 97 | +class MyComp extends Component { |
| 98 | + |
| 99 | +} |
| 100 | +``` |
0 commit comments