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

212 lines
5.4 KiB
React
Raw Normal View History

import React, { Component } from 'react';
import { withTracker } from 'meteor/react-meteor-data';
import PropTypes from 'prop-types';
import { IntlProvider, addLocaleData } 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
// currently supported locales.
import ar from 'react-intl/locale-data/ar';
2020-04-17 20:02:41 +08:00
import az from 'react-intl/locale-data/az';
import bg from 'react-intl/locale-data/bg';
2020-03-25 04:23:16 +08:00
import ca from 'react-intl/locale-data/ca';
import cs from 'react-intl/locale-data/cs';
import da from 'react-intl/locale-data/da';
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';
2020-03-25 04:23:16 +08:00
import et from 'react-intl/locale-data/et';
2019-04-24 21:08:11 +08:00
import eu from 'react-intl/locale-data/eu';
import fa from 'react-intl/locale-data/fa';
import fi from 'react-intl/locale-data/fi';
import fr from 'react-intl/locale-data/fr';
import gl from 'react-intl/locale-data/gl';
import he from 'react-intl/locale-data/he';
2019-04-24 21:08:11 +08:00
import hi from 'react-intl/locale-data/hi';
import hr from 'react-intl/locale-data/hr';
import hu from 'react-intl/locale-data/hu';
2020-04-17 20:02:41 +08:00
import hy from 'react-intl/locale-data/hy';
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 ka from 'react-intl/locale-data/ka';
import km from 'react-intl/locale-data/km';
2020-05-08 18:27:23 +08:00
import kn from 'react-intl/locale-data/kn';
2019-11-16 06:13:43 +08:00
import ko from 'react-intl/locale-data/ko';
2020-03-25 04:23:16 +08:00
import lt from 'react-intl/locale-data/lt';
import lv from 'react-intl/locale-data/lv';
import nb from 'react-intl/locale-data/nb';
2019-11-16 06:13:43 +08:00
import nl from 'react-intl/locale-data/nl';
import pl from 'react-intl/locale-data/pl';
import pt from 'react-intl/locale-data/pt';
import ro from 'react-intl/locale-data/ro';
import ru from 'react-intl/locale-data/ru';
2019-11-16 06:13:43 +08:00
import sk from 'react-intl/locale-data/sk';
import sl from 'react-intl/locale-data/sl';
2019-11-16 06:13:43 +08:00
import sr from 'react-intl/locale-data/sr';
2019-05-09 22:47:47 +08:00
import sv from 'react-intl/locale-data/sv';
import th from 'react-intl/locale-data/th';
import tr from 'react-intl/locale-data/tr';
import uk from 'react-intl/locale-data/uk';
import vi from 'react-intl/locale-data/vi';
import zh from 'react-intl/locale-data/zh';
2019-01-10 05:43:54 +08:00
addLocaleData([
...ar,
2020-04-17 20:02:41 +08:00
...az,
...bg,
2020-03-25 04:23:16 +08:00
...ca,
...cs,
...da,
...de,
...el,
2020-03-25 04:23:16 +08:00
...et,
...en,
...es,
2019-04-24 21:08:11 +08:00
...eu,
...fa,
...fi,
...fr,
...gl,
...he,
2019-04-24 21:08:11 +08:00
...hi,
...hr,
...hu,
2020-04-17 20:02:41 +08:00
...hy,
...id,
...it,
...ja,
...ka,
...km,
2020-05-08 18:27:23 +08:00
...kn,
2019-11-16 06:13:43 +08:00
...ko,
2020-03-25 04:23:16 +08:00
...lt,
...lv,
...nb,
2019-11-16 06:13:43 +08:00
...nl,
...pl,
...pt,
...ro,
...ru,
2019-11-16 06:13:43 +08:00
...sk,
...sl,
2019-11-16 06:13:43 +08:00
...sr,
2019-05-09 22:47:47 +08:00
...sv,
...th,
...tr,
...uk,
...vi,
...zh,
]);
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
const RTL_LANGUAGES = ['ar', 'he', 'fa'];
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
if (RTL_LANGUAGES.includes(props.locale)) {
document.body.parentNode.setAttribute('dir', 'rtl');
}
2017-03-11 02:33:46 +08:00
this.fetchLocalizedMessages = this.fetchLocalizedMessages.bind(this);
}
2018-12-06 01:42:31 +08:00
componentWillMount() {
const { locale } = this.props;
this.fetchLocalizedMessages(locale, true);
}
componentWillUpdate(nextProps) {
const { fetching, normalizedLocale } = this.state;
const { locale } = nextProps;
if (!fetching
&& normalizedLocale
&& locale.toLowerCase() !== normalizedLocale.toLowerCase()) {
this.fetchLocalizedMessages(locale);
}
}
2017-03-11 02:33:46 +08:00
fetchLocalizedMessages(locale, init = false) {
const url = `/html5client/locale?locale=${locale}&init=${init}`;
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 }, () => {
this.saveLocale(dasherizedLocale);
});
})
.catch(() => {
this.setState({ fetching: false, normalizedLocale: null }, () => {
this.saveLocale(DEFAULT_LANGUAGE);
});
2017-11-01 01:13:44 +08:00
});
});
2017-03-11 02:33:46 +08:00
}
saveLocale(localeName) {
Settings.application.locale = localeName;
if (RTL_LANGUAGES.includes(localeName.substring(0, 2))) {
document.body.parentNode.setAttribute('dir', 'rtl');
Settings.application.isRTL = true;
} else {
document.body.parentNode.setAttribute('dir', 'ltr');
Settings.application.isRTL = false;
}
Settings.save();
}
2017-03-11 02:33:46 +08:00
render() {
const { fetching, normalizedLocale, messages } = this.state;
const { children } = this.props;
return fetching ? <LoadingScreen /> : (
<IntlProvider locale={normalizedLocale} messages={messages}>
{children}
2017-03-11 02:33:46 +08:00
</IntlProvider>
);
}
2017-06-03 03:25:02 +08:00
}
2017-03-11 02:33:46 +08:00
const IntlStartupContainer = withTracker(() => {
const { locale } = Settings.application;
return {
locale,
};
})(IntlStartup);
export default IntlStartupContainer;
2017-03-11 02:33:46 +08:00
IntlStartup.propTypes = propTypes;
IntlStartup.defaultProps = defaultProps;