-
Notifications
You must be signed in to change notification settings - Fork 31
/
index.js
184 lines (144 loc) · 4.61 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
'use strict';
var React = require('react');
var PropTypes = require('prop-types');
var createReactClass = require('create-react-class');
var Interpolate = require('react-interpolate-component');
var translator = require('counterpart');
var extend = require('object-assign');
var translatorType = PropTypes.shape({
getLocale: PropTypes.func,
onLocaleChange: PropTypes.func,
offLocaleChange: PropTypes.func,
translate: PropTypes.func
});
var keyType = PropTypes.oneOfType([
PropTypes.string,
PropTypes.arrayOf(PropTypes.string)
]);
var Translate = createReactClass({
displayName: 'Translate',
contextTypes: {
translator: translatorType,
scope: keyType
},
propTypes: {
locale: PropTypes.string,
count: PropTypes.number,
content: keyType,
scope: keyType,
attributes: PropTypes.object,
with: PropTypes.object
},
statics: {
textContentComponents: ['title', 'option', 'textarea']
},
getDefaultProps: function() {
return { component: 'span' };
},
getInitialState: function() {
return { locale: this.getTranslator().getLocale() };
},
getTranslator: function() {
return this.context.translator || translator;
},
componentDidMount: function() {
if (!this.props.locale) {
this.getTranslator().onLocaleChange(this.localeChanged);
}
},
componentWillUnmount: function() {
if (!this.props.locale) {
this.getTranslator().offLocaleChange(this.localeChanged);
}
},
localeChanged: function(newLocale) {
this.setState({ locale: newLocale });
},
render: function() {
var props = extend({}, this.props);
var translator = this.getTranslator();
var textContent = Translate.textContentComponents.indexOf(props.component) > -1;
var interpolate = textContent || props.unsafe === true;
var interpolations = props.with;
var attributeKey;
var attributeTranslationOptions = extend(
{ locale: this.state.locale, scope: this.context.scope },
props,
interpolations,
{ interpolate: true }
);
var contentTranslationOptions = extend(
{},
attributeTranslationOptions,
{ interpolate: interpolate }
);
delete props.locale;
delete props.scope;
delete props.count;
delete props.with;
delete props.fallback;
if (props.attributes) {
for (var name in props.attributes) {
attributeKey = props.attributes[name];
if (attributeKey) {
props[name] = translator.translate(attributeKey, attributeTranslationOptions);
}
}
delete props.attributes;
}
if (props.content) {
var translation = translator.translate(props.content, contentTranslationOptions);
var interpolateProps = extend({}, props, { with: interpolations });
delete interpolateProps.content;
delete interpolateProps.children;
return React.createElement(Interpolate, interpolateProps, translation);
} else {
var component = props.component;
delete props.component;
delete props.unsafe;
return React.createElement(component, props);
}
}
});
module.exports = Translate;
module.exports.translate = function(key, options) {
return React.createElement(Translate, extend({}, options, { content: key }));
};
module.exports.translatorType = translatorType;
module.exports.getLocale = translator.getLocale.bind(translator);
module.exports.setLocale = translator.setLocale.bind(translator);
module.exports.onLocaleChange = translator.onLocaleChange.bind(translator);
module.exports.offLocaleChange = translator.offLocaleChange.bind(translator);
module.exports.registerTranslations = translator.registerTranslations.bind(translator);
function withTranslations(DecoratedComponent, translations) {
if (!translations) {
return function(decoratedComponent) {
return withTranslations(decoratedComponent, DecoratedComponent);
};
}
var displayName =
DecoratedComponent.displayName ||
DecoratedComponent.name ||
'Component';
for (var locale in translations) {
var localeTranslations = translations[locale];
var scopedTranslations = {};
scopedTranslations[displayName] = localeTranslations;
translator.registerTranslations(locale, scopedTranslations);
}
return createReactClass({
displayName: displayName + 'WithTranslations',
childContextTypes: {
scope: keyType
},
getChildContext: function() {
return {
scope: displayName
};
},
render: function() {
return React.createElement(DecoratedComponent, this.props);
}
});
}
module.exports.withTranslations = withTranslations;