forked from jenseng/react-i18nliner
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Translate.js
65 lines (54 loc) · 1.78 KB
/
Translate.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
/*
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})">
<Link />
<b><i /></b>
</ComponentInterpolator>
*/
var React = require('react');
var I18n = require('i18n');
var invariant = require('react/lib/invariant');
var { string } = React.PropTypes;
var OWN_PROPS = ['defaultValue', 'translateKey', 'children'];
var Translate = React.createClass({
propTypes: {
translateKey: string,
defaultValue: string
},
componentWillMount() {
var textCount = this.textCount();
var componentCount = this.componentCount();
invariant(
textCount <= 1,
'<Translate> can only have one text child when not using pre-processing'
);
invariant(
componentCount === 0,
'<Translate> cannot have any component children when not using pre-processing'
);
invariant(
textCount === 1 || this.props.defaultValue || this.props.translateKey,
'<Translate> needs at least a translateKey, defaultValue, or text child'
);
},
textCount(node) {
node = node || this;
count = 0;
React.Children.forEach(node.props.children, function(child) {
count += typeof child === 'string' ? 1 : this.textCount(child);
});
return count;
},
render() {
var options = this.extraProps();
var translateKey = this.props.translateKey;
var defaultValue = this.props.defaultValue || this.props.children;
options.defaultValue = defaultValue;
var string = I18n.t(translateKey, options);
var children = this.inferChildren(string, this.props.children);
return React.createElement('span', {}, children);
}
});
module.exports = Translate;