-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
localize.js
38 lines (31 loc) · 1.1 KB
/
localize.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
import PropTypes from 'prop-types';
import React, {Component} from 'react';
import {getDisplayName} from 'lib';
export default function localize(Comp) {
class LocalizedComponent extends Component {
constructor(props, context) {
super(props, context);
const dictionaries = context.dictionaries;
const locale = context.locale;
this.localize = function localize(str) {
return localizeString(dictionaries, locale, str);
};
}
render() {
return <Comp localize={this.localize} {...this.props} />;
}
}
LocalizedComponent.displayName = `Localized${getDisplayName(Comp)}`;
LocalizedComponent.contextTypes = LocalizedComponent.contextTypes || {};
LocalizedComponent.contextTypes.dictionaries = PropTypes.object;
LocalizedComponent.contextTypes.locale = PropTypes.string;
LocalizedComponent.plotly_editor_traits = Comp.plotly_editor_traits;
return LocalizedComponent;
}
export function localizeString(dictionaries, locale, key) {
const dict = dictionaries[locale];
if (dict && dict.hasOwnProperty(key)) {
return dict[key];
}
return key;
}