bigbluebutton-Github/bigbluebutton-html5/imports/startup/client/intl.js

74 lines
1.7 KiB
JavaScript
Raw Normal View History

2017-03-11 02:33:46 +08:00
import React, { Component, PropTypes } from 'react';
import { IntlProvider } from 'react-intl';
const propTypes = {
locale: PropTypes.string.isRequired,
};
const defaultProps = {
locale: 'en',
};
class IntlStartup extends Component {
constructor(props) {
super(props);
this.state = {
messages: {},
2017-05-18 00:02:13 +08:00
appLocale: this.props.locale,
2017-03-11 02:33:46 +08:00
};
2017-05-18 00:02:13 +08:00
2017-03-11 02:33:46 +08:00
this.fetchLocalizedMessages = this.fetchLocalizedMessages.bind(this);
}
fetchLocalizedMessages(locale) {
const url = `/html5client/locale?locale=${locale}`;
const { baseControls } = this.props;
baseControls.updateLoadingState(true);
fetch(url)
2017-05-16 00:32:41 +08:00
.then(response => {
if (response.ok) {
return response.json();
} else {
2017-05-18 00:02:13 +08:00
this.setState({ appLocale: 'en' });
2017-05-16 00:32:41 +08:00
return response.json();
}
})
2017-03-11 02:33:46 +08:00
.then(messages => {
this.setState({ messages }, () => {
baseControls.updateLoadingState(false);
});
})
.catch(reason => {
baseControls.updateErrorState(reason);
baseControls.updateLoadingState(false);
});
}
componentWillMount() {
2017-05-16 03:45:00 +08:00
this.fetchLocalizedMessages(this.state.appLocale);
2017-03-11 02:33:46 +08:00
}
componentWillUpdate(nextProps, nextState) {
if (this.props.locale !== nextProps.locale) {
2017-05-18 00:02:13 +08:00
this.setState({ appLocale: nextProps.locale });
2017-05-16 03:45:00 +08:00
this.fetchLocalizedMessages(nextProps.locale);
2017-03-11 02:33:46 +08:00
}
}
render() {
return (
2017-05-16 03:45:00 +08:00
<IntlProvider locale={this.state.appLocale} messages={this.state.messages}>
2017-03-11 02:33:46 +08:00
{this.props.children}
</IntlProvider>
);
}
};
export default IntlStartup;
IntlStartup.propTypes = propTypes;
IntlStartup.defaultProps = defaultProps;