Skip to content

Commit 89b5e92

Browse files
committed
Release 0.2.6
1 parent 15b0a6b commit 89b5e92

File tree

5 files changed

+77
-9
lines changed

5 files changed

+77
-9
lines changed

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-translate-maker",
3-
"version": "0.2.5",
3+
"version": "0.2.6",
44
"description": "Universal internationalization (i18n) open source library for React",
55
"main": "dist/index.js",
66
"keywords": [
@@ -46,8 +46,10 @@
4646
},
4747
"homepage": "https://github.com/CherryProjects/react-translate-maker",
4848
"dependencies": {
49+
"lodash": "^4.14.1",
4950
"translate-maker": "^0.4.5",
50-
"react-provide-props": "^2.0.3"
51+
"react-provide-props": "^2.0.3",
52+
"react-addons-test-utils": "^15.3.0"
5153
},
5254
"devDependencies": {
5355
"babel-cli": "^6.8.0",

src/LocaleProvider.jsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ export default class LocaleProvider extends Component {
3737
translate,
3838
locale: translate.getLocale(),
3939
};
40-
41-
this.t = this.t.bind(this);
4240
}
4341

4442
componentDidMount() {
@@ -64,7 +62,7 @@ export default class LocaleProvider extends Component {
6462
}
6563
}
6664

67-
t(path, attrs, defaultValue) {
65+
t = (path, attrs, defaultValue) => {
6866
return this.get(path, attrs, defaultValue);
6967
}
7068

src/Translate.jsx

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
11
import React, { Component, PropTypes } from 'react';
2+
import { renderToStaticMarkup } from 'react/lib/ReactDOMServer';
23
import LocaleProvider from './LocaleProvider';
4+
import forEach from 'lodash/forEach';
5+
import { isElement } from 'react-addons-test-utils';
6+
7+
export function prepareProps(props, localeProvider) {
8+
const newProps = {};
9+
let changed = false;
10+
11+
forEach(props, (value, key) => {
12+
const isReactElement = isElement(value);
13+
if (!isReactElement) {
14+
newProps[key] = value;
15+
return;
16+
}
17+
18+
changed = true;
19+
newProps[key] = renderToStaticMarkup(
20+
<LocaleProvider {...localeProvider.props} children={value} />
21+
);
22+
});
23+
24+
return changed ? newProps : props;
25+
}
326

427
export default class Translate extends Component {
528
static contextTypes = {
@@ -36,13 +59,19 @@ export default class Translate extends Component {
3659
return `${parentPath}.${path}`;
3760
}
3861

62+
63+
3964
render() {
4065
const { tagName, params, defaultValue, className, props = {} } = this.props;
4166

4267
const path = this.getPath();
4368

44-
const translate = this.context.translate;
45-
const text = translate.get(path, params || this.props, defaultValue);
69+
const { translate } = this.context;
70+
71+
const currentProps = params || this.props;
72+
const updatedProps = prepareProps(currentProps, translate);
73+
const isChanged = currentProps !== updatedProps;
74+
const text = translate.get(path, updatedProps, defaultValue);
4675

4776
if (className && !props.className) {
4877
props.className = className;

src/TranslateHTML.jsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React, { Component, PropTypes } from 'react';
22
import LocaleProvider from './LocaleProvider';
3+
import { prepareProps } from './Translate';
34

45
export default class TranslateHTML extends Component {
56
static contextTypes = {
@@ -41,9 +42,12 @@ export default class TranslateHTML extends Component {
4142
const { tagName, params, defaultValue, props = {}, children, className } = this.props;
4243

4344
const path = this.getPath();
45+
const { translate } = this.context;
4446

45-
const translate = this.context.translate;
46-
const text = translate.get(path, params || this.props, defaultValue);
47+
const currentProps = params || this.props;
48+
const updatedProps = prepareProps(currentProps, translate);
49+
const isChanged = currentProps !== updatedProps;
50+
const text = translate.get(path, updatedProps, defaultValue);
4751

4852
const elementProps = {
4953
className,

tests/test.jsx

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,4 +340,39 @@ describe('Translate', () => {
340340
const content = findDOMNode(node).querySelector('div');
341341
findDOMNode(node).innerHTML.should.equal('Testovacia odpoved');
342342
});
343+
344+
it('should be able to other components as props', () => {
345+
const adapter = {
346+
sk_SK: {
347+
test: 'Testovacia odpoved {$link}',
348+
},
349+
};
350+
351+
const node = renderJSX(
352+
<LocaleProvider locale="sk_SK" adapter={adapter}>
353+
<TranslateHTML path="test" link={<a>Asdf</a>} />
354+
</LocaleProvider>
355+
);
356+
357+
const content = findDOMNode(node).querySelector('div');
358+
findDOMNode(node).innerHTML.should.equal('Testovacia odpoved <a>Asdf</a>');
359+
});
360+
361+
it('should be able to other components as props with Translate', () => {
362+
const adapter = {
363+
sk_SK: {
364+
test: 'Testovacia odpoved {$link}',
365+
inner: '123',
366+
},
367+
};
368+
369+
const node = renderJSX(
370+
<LocaleProvider locale="sk_SK" adapter={adapter}>
371+
<TranslateHTML path="test" link={<a>Asdf <Translate path="inner" /></a>} />
372+
</LocaleProvider>
373+
);
374+
375+
const content = findDOMNode(node).querySelector('div');
376+
findDOMNode(node).innerHTML.should.equal('Testovacia odpoved <a>Asdf <span>123</span></a>');
377+
});
343378
});

0 commit comments

Comments
 (0)