Skip to content

Commit 767e949

Browse files
committed
test: add translation story
1 parent 6b327ab commit 767e949

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
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>`, `withTranslations`, and `@withTranslations`
75+
- `<Translations>`, `<Translate>`, `<T>`, `withT()`, and `@withT`
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)

src/translate/__story__/story.tsx

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,15 @@ import {action} from '@storybook/addon-actions';
44
import {linkTo} from '@storybook/addon-links';
55
import {LocationSensor} from '../../LocationSensor';
66
import {Translations, Translate, T, withT} from '..';
7-
import ShowDocs from '../../../.storybook/ShowDocs'
7+
import ShowDocs from '../../../.storybook/ShowDocs';
8+
9+
const Demo = ({T}) => {
10+
return (
11+
<span>{T('foo')}: {T('hello')}</span>
12+
);
13+
};
14+
15+
const Hoc1 = withT(Demo);
816

917
storiesOf('Context/translate', module)
1018
.add('Documentation', () => h(ShowDocs, {name: 'translate'}))
@@ -14,8 +22,40 @@ storiesOf('Context/translate', module)
1422
}}>
1523
<div>
1624
<T>{(T) =>
17-
<span>{T('omg')}: {T('hello')}</span>
25+
<Demo T={T} />
1826
}</T>
1927
</div>
2028
</Translations>
29+
))
30+
.add('HOC', () => (
31+
<Translations map={{
32+
foo: 'bar',
33+
hello: (T) => `Hello, ${T('foo')}`
34+
}}>
35+
<div>
36+
<Hoc1 />
37+
</div>
38+
</Translations>
39+
))
40+
.add('Multiple namespaces', () => (
41+
<Translations ns='a' map={{
42+
foo: 'bar'
43+
}}>
44+
<Translations ns='b' map={{
45+
foo: 'baz'
46+
}}>
47+
<div>
48+
<div>
49+
<T ns='a'>{(T) =>
50+
<span>{T('foo')}</span>
51+
}</T>
52+
</div>
53+
<div>
54+
<T ns='b'>{(T) =>
55+
<span>{T('foo')}</span>
56+
}</T>
57+
</div>
58+
</div>
59+
</Translations>
60+
</Translations>
2161
));

src/translate/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import faccToHoc from '../util/faccToHoc';
55
import renderProp from '../util/renderProp';
66

77
export interface ITranslationsProps {
8-
map: {[key: string]: string | React.StatelessComponent<any>};
8+
map: {[key: string]: string | ((...args) => string)};
99
ns?: string;
1010
}
1111

@@ -33,11 +33,11 @@ export interface ITranslateState {
3333
export class Translate extends Component<ITranslateProps, ITranslateState> {
3434
render () {
3535
return h(Consumer, {name: ns(`T/${this.props.ns}`)}, (map) => {
36-
const T = (key) => {
36+
const T = (key, ...args) => {
3737
const value = map[key];
3838

3939
if (typeof value === 'function') {
40-
return value(T);
40+
return value(T, ...args);
4141
}
4242

4343
return value || key;

0 commit comments

Comments
 (0)