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

77 lines
1.8 KiB
React
Raw Normal View History

import React, { Component } from 'react';
import PropTypes from 'prop-types';
2017-03-11 02:33:46 +08:00
import { IntlProvider } from 'react-intl';
const propTypes = {
locale: PropTypes.string.isRequired,
baseControls: PropTypes.object.isRequired,
children: PropTypes.object.isRequired,
2017-03-11 02:33:46 +08:00
};
2017-06-06 20:43:57 +08:00
const BROWSER_LANGUAGE = window.navigator.userLanguage || window.navigator.language;
2017-03-11 02:33:46 +08:00
const defaultProps = {
2017-06-06 20:43:57 +08:00
locale: BROWSER_LANGUAGE,
2017-03-11 02:33:46 +08:00
};
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);
}
componentWillMount() {
this.fetchLocalizedMessages(this.state.appLocale);
}
componentWillUpdate(nextProps) {
if (this.props.locale !== nextProps.locale) {
this.fetchLocalizedMessages(nextProps.locale);
}
}
2017-03-11 02:33:46 +08:00
fetchLocalizedMessages(locale) {
const url = `/html5client/locale?locale=${locale}`;
const { baseControls } = this.props;
this.setState({ appLocale: locale });
2017-03-11 02:33:46 +08:00
baseControls.updateLoadingState(true);
fetch(url)
2017-06-03 03:25:02 +08:00
.then((response) => {
2017-05-16 00:32:41 +08:00
if (response.ok) {
return response.json();
}
2017-06-03 03:25:02 +08:00
this.setState({ appLocale: 'en' });
return response.json();
2017-05-16 00:32:41 +08:00
})
2017-06-03 03:25:02 +08:00
.then((messages) => {
2017-03-11 02:33:46 +08:00
this.setState({ messages }, () => {
baseControls.updateLoadingState(false);
});
})
2017-06-03 03:25:02 +08:00
.catch((reason) => {
2017-03-11 02:33:46 +08:00
baseControls.updateErrorState(reason);
baseControls.updateLoadingState(false);
});
}
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>
);
}
2017-06-03 03:25:02 +08:00
}
2017-03-11 02:33:46 +08:00
export default IntlStartup;
IntlStartup.propTypes = propTypes;
IntlStartup.defaultProps = defaultProps;