forked from jenseng/react-i18nliner
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ComponentInterpolator.js
77 lines (69 loc) · 1.85 KB
/
ComponentInterpolator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
* Given:
*
* <Translate>Ohai {this.props.user}, click <Link>here</Link> right <b>now <i>please</i></b>!</Translate>
*
* Pre-process it into:
*
* <ComponentInterpolator
* string={I18n.t("Ohai, %{user}, click *here* right ***now **please** ***", {user: this.props.user})"
* wrappers={{
* '*': <Link/>,
* '**': <b/>,
* '***': <i/>}}
* />
*
* Which is equivalent to:
*
* <span>Ohai {this.props.user}, click <Link>here</Link> right <b>now <i>please</i></b>!</span>
*
* ... but completely localizable :)
*/
var React = require('react');
var cloneWithProps = require('react/lib/cloneWithProps');
var invariant = require('react/lib/invariant');
var { string, object } = React.PropTypes;
var ComponentInterpolator = React.createClass({
propTypes: {
string: string.isRequired,
wrappers: object.isRequired
},
componentWillMount() {
invariant(
!this.props.children,
'<ComponentInterpolator> cannot have any children'
);
},
inferChildren() {
var tokens = (this.props.string || '').split(/(\*+)/);
return this.interpolateComponents(tokens);
},
interpolateComponents(tokens, eof) {
var token, child
var children = [];
var wrappers = this.props.wrappers || {};
while (tokens.length) {
token = tokens.shift();
if (token === eof) break;
if (token.match(/\*/)) {
invariant(
child = wrappers[token],
`<ComponentInterpolator>'s string expected ${token} wrapper, none found`
)
child = cloneWithProps(child, {
key: token,
children: this.interpolateComponents(tokens, token)
});
}
else {
child = token;
}
children.push(child);
}
return children;
},
render() {
return React.createElement('span', {}, this.inferChildren());
}
});
module.exports = ComponentInterpolator;