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

83 lines
2.2 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';
2017-11-01 01:13:44 +08:00
import Settings from '/imports/ui/services/settings';
import LoadingScreen from '/imports/ui/components/loading-screen/component';
2017-03-11 02:33:46 +08:00
const propTypes = {
locale: PropTypes.string,
2017-10-24 08:09:40 +08:00
children: PropTypes.element.isRequired,
2017-03-11 02:33:46 +08:00
};
2017-10-27 21:23:18 +08:00
const DEFAULT_LANGUAGE = Meteor.settings.public.app.defaultSettings.application.locale;
2017-06-06 20:43:57 +08:00
2017-03-11 02:33:46 +08:00
const defaultProps = {
2017-11-01 01:13:44 +08:00
locale: DEFAULT_LANGUAGE,
2017-03-11 02:33:46 +08:00
};
class IntlStartup extends Component {
constructor(props) {
super(props);
this.state = {
messages: {},
normalizedLocale: null,
fetching: false,
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() {
2017-10-27 19:36:27 +08:00
this.fetchLocalizedMessages(this.props.locale);
}
componentWillUpdate(nextProps) {
if (!this.state.fetching
&& this.state.normalizedLocale
&& nextProps.locale.toLowerCase() !== this.state.normalizedLocale.toLowerCase()) {
this.fetchLocalizedMessages(nextProps.locale);
}
}
2017-03-11 02:33:46 +08:00
fetchLocalizedMessages(locale) {
const url = `/html5client/locale?locale=${locale}`;
this.setState({ fetching: true }, () => {
fetch(url)
.then((response) => {
if (!response.ok) {
return Promise.reject();
}
2017-03-11 02:33:46 +08:00
return response.json();
})
.then(({ messages, normalizedLocale }) => {
const dasherizedLocale = normalizedLocale.replace('_', '-');
this.setState({ messages, fetching: false, normalizedLocale: dasherizedLocale }, () => {
Settings.application.locale = dasherizedLocale;
Settings.save();
});
})
.catch(() => {
this.setState({ fetching: false, normalizedLocale: null }, () => {
Settings.application.locale = DEFAULT_LANGUAGE;
Settings.save();
});
2017-11-01 01:13:44 +08:00
});
});
2017-03-11 02:33:46 +08:00
}
render() {
return this.state.fetching ? <LoadingScreen /> : (
<IntlProvider locale={this.state.normalizedLocale} 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;