Skip to content

Commit 89c1503

Browse files
committed
docs: add translation docs
1 parent 767e949 commit 89c1503

File tree

2 files changed

+101
-1
lines changed

2 files changed

+101
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ const MyComponent = mock();
7272
- [`<Theme>`](./docs/theme.md#theme), [`<Themed>`](./docs/theme.md#themed), [`withTheme()`](./docs/theme.md#withtheme), and `@withTheme`
7373
- `<CssVars>`
7474
- [`<Router>`](./docs/route.md#router), [`<Route>`](./docs/route.md#route), [`withRoute()`](./docs/route.md#withroute), `@withRoute`, `go()`, and `<Go>`
75-
- `<Translations>`, `<Translate>`, `<T>`, `withT()`, and `@withT`
75+
- [`<Translations>`](./docs/translate.md#translations), [`<Translate>`](./docs/translate.md#translate), [`<T>`](./docs/translate.md#translate), [`withT()`](./docs/translate.md#witht-hoc), and [`@withT`](./docs/translate.md#witht-decorator)
7676
- [CSS resets](./docs/CSS-resets.md)
7777
- [`<CssResetEricMeyer>`](./docs/reset/CssResetEricMeyer.md) and [`<CssResetEricMeyerCondensed>`](./docs/reset/CssResetEricMeyerCondensed.md)
7878
- [`<CssResetMinimalistic>`](./docs/reset/CssResetMinimalistic.md), [`<CssResetMinimalistic2>`](./docs/reset/CssResetMinimalistic2.md), and [`<CssResetMinimalistic3>`](./docs/reset/CssResetMinimalistic3.md)

docs/translate.md

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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

Comments
 (0)