-
Notifications
You must be signed in to change notification settings - Fork 6
/
provider.js
44 lines (34 loc) · 998 Bytes
/
provider.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
'use strict'
var React = require('react')
var PropTypes = require('prop-types')
var nanotranslate = require('nanotranslate')
module.exports = Provider
Provider.prototype = Object.create(React.Component.prototype)
function Provider (props) {
React.Component.call(this, props)
this.onUpdate(props.dictionary)
}
Provider.childContextTypes = {
translate: PropTypes.func.isRequired
}
Provider.propTypes = {
dictionary: PropTypes.shape({
id: PropTypes.string.isRequired,
values: PropTypes.object.isRequired
})
}
Provider.prototype.getChildContext = function () {
return {
translate: this.translate
}
}
Provider.prototype.componentWillReceiveProps = function (nextProps) {
if (nextProps.dictionary === this.props.dictionary) return
this.onUpdate(nextProps.dictionary)
}
Provider.prototype.onUpdate = function (dictionary) {
this.translate = nanotranslate(dictionary)
}
Provider.prototype.render = function () {
return React.Children.only(this.props.children)
}