-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
52 lines (39 loc) · 1.12 KB
/
index.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
'use strict'
let React = require('react')
let X18N = require('x18n')
let X18NSpanElement = React.createClass({
getDefaultProps: function () {
return {isPlural: false, _args: []}
},
_update: function () {
this.forceUpdate()
},
componentWillMount: function () {
X18N.on(['lang:change', 'dict:change'], this._update)
},
componentWillUnmount: function () {
X18N.off(['lang:change', 'dict:change'], this._update)
},
render: function () {
let result = 'error'
if (this.props.isPlural === true) {
let args = Array.prototype.slice.call(this.props._args)
let key = args.shift()
result = X18N.t(key).plural.apply(null, args)
} else {
result = X18N.t.apply(null, this.props._args)
}
return React.createElement('span', {}, result)
}
})
let react_x18n_t = function () {
return React.createElement(X18NSpanElement, {isPlural: false, _args: arguments}, '')
}
react_x18n_t.plural = function () {
return React.createElement(X18NSpanElement, {isPlural: true, _args: arguments}, '')
}
let react_x18n = {
x18n: X18N,
t: react_x18n_t
}
module.exports = react_x18n