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

147 lines
3.9 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 } 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';
import getFromUserSettings from '/imports/ui/services/users-settings';
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
};
2020-08-25 20:29:23 +08:00
const DEFAULT_LANGUAGE = Meteor.settings.public.app.defaultSettings.application.fallbackLocale;
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 {
static saveLocale(localeName) {
if (Settings.application.locale !== localeName) {
Settings.application.changedLocale = 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
constructor(props) {
super(props);
this.state = {
messages: {},
normalizedLocale: null,
fetching: true,
2020-08-25 20:29:23 +08:00
localeChanged: 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
componentDidMount() {
const { locale } = this.props;
this.fetchLocalizedMessages(locale, true);
}
2020-08-25 20:29:23 +08:00
componentDidUpdate(prevProps) {
const { fetching, normalizedLocale, localeChanged } = this.state;
const { locale, overrideLocale, changedLocale } = this.props;
2020-08-25 20:29:23 +08:00
if (prevProps.locale !== locale) {
this.setState({
localeChanged: true,
});
}
if (overrideLocale) {
if (!fetching
&& (overrideLocale !== normalizedLocale.toLowerCase())
&& !localeChanged
&& !changedLocale) {
this.fetchLocalizedMessages(overrideLocale);
}
if (!localeChanged) {
return;
}
}
if (!fetching
&& normalizedLocale
2020-08-25 20:29:23 +08:00
&& ((locale.toLowerCase() !== normalizedLocale.toLowerCase()))) {
if (((DEFAULT_LANGUAGE === normalizedLocale.toLowerCase()) && !localeChanged)) return;
this.fetchLocalizedMessages(locale);
}
}
2017-03-11 02:33:46 +08:00
fetchLocalizedMessages(locale, init = false) {
const url = `./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 }, () => {
IntlStartup.saveLocale(dasherizedLocale);
});
})
.catch(() => {
this.setState({ fetching: false, normalizedLocale: null }, () => {
IntlStartup.saveLocale(DEFAULT_LANGUAGE);
});
2017-11-01 01:13:44 +08:00
});
});
2017-03-11 02:33:46 +08:00
}
2017-03-11 02:33:46 +08:00
render() {
const { fetching, normalizedLocale, messages } = this.state;
const { children } = this.props;
return (fetching || !normalizedLocale) ? <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, changedLocale } = Settings.application;
const overrideLocale = getFromUserSettings('bbb_override_default_locale', null);
return {
locale,
overrideLocale,
changedLocale,
};
})(IntlStartup);
export default IntlStartupContainer;
2017-03-11 02:33:46 +08:00
IntlStartup.propTypes = propTypes;
IntlStartup.defaultProps = defaultProps;