bigbluebutton-Github/bigbluebutton-html5/imports/startup/client/intl.jsx
2019-04-24 13:08:11 +00:00

141 lines
3.6 KiB
JavaScript

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { IntlProvider, addLocaleData } from 'react-intl';
import Settings from '/imports/ui/services/settings';
import LoadingScreen from '/imports/ui/components/loading-screen/component';
// currently supported locales.
import ar from 'react-intl/locale-data/ar';
import bg from 'react-intl/locale-data/bg';
import cs from 'react-intl/locale-data/cs';
import de from 'react-intl/locale-data/de';
import el from 'react-intl/locale-data/el';
import en from 'react-intl/locale-data/en';
import es from 'react-intl/locale-data/es';
import eu from 'react-intl/locale-data/eu';
import fa from 'react-intl/locale-data/fa';
import fr from 'react-intl/locale-data/fr';
import he from 'react-intl/locale-data/he';
import hi from 'react-intl/locale-data/hi';
import id from 'react-intl/locale-data/id';
import it from 'react-intl/locale-data/it';
import ja from 'react-intl/locale-data/ja';
import km from 'react-intl/locale-data/km';
import pl from 'react-intl/locale-data/pl';
import pt from 'react-intl/locale-data/pt';
import ru from 'react-intl/locale-data/ru';
import tr from 'react-intl/locale-data/tr';
import uk from 'react-intl/locale-data/uk';
import zh from 'react-intl/locale-data/zh';
addLocaleData([
...ar,
...bg,
...cs,
...de,
...el,
...en,
...es,
...eu,
...fa,
...fr,
...he,
...hi,
...id,
...it,
...ja,
...km,
...pl,
...pt,
...ru,
...tr,
...uk,
...zh,
]);
const propTypes = {
locale: PropTypes.string,
children: PropTypes.element.isRequired,
};
const DEFAULT_LANGUAGE = Meteor.settings.public.app.defaultSettings.application.locale;
const defaultProps = {
locale: DEFAULT_LANGUAGE,
};
class IntlStartup extends Component {
constructor(props) {
super(props);
this.state = {
messages: {},
normalizedLocale: null,
fetching: false,
};
this.fetchLocalizedMessages = this.fetchLocalizedMessages.bind(this);
}
componentWillMount() {
const { locale } = this.props;
this.fetchLocalizedMessages(locale);
}
componentWillUpdate(nextProps) {
const { fetching, normalizedLocale } = this.state;
const { locale } = nextProps;
if (!fetching
&& normalizedLocale
&& locale.toLowerCase() !== normalizedLocale.toLowerCase()) {
this.fetchLocalizedMessages(locale);
}
}
fetchLocalizedMessages(locale) {
const url = `/html5client/locale?locale=${locale}`;
this.setState({ fetching: true }, () => {
fetch(url)
.then((response) => {
if (!response.ok) {
return Promise.reject();
}
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();
});
});
});
}
render() {
const { fetching, normalizedLocale, messages } = this.state;
const { children } = this.props;
return fetching ? <LoadingScreen /> : (
<IntlProvider locale={normalizedLocale} messages={messages}>
{children}
</IntlProvider>
);
}
}
export default IntlStartup;
IntlStartup.propTypes = propTypes;
IntlStartup.defaultProps = defaultProps;