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

123 lines
3.5 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';
import _ from 'lodash';
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) {
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,
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, overrideLocaleFromPassedParameter } = this.props;
this.fetchLocalizedMessages(overrideLocaleFromPassedParameter || locale, true);
}
2020-08-25 20:29:23 +08:00
componentDidUpdate(prevProps) {
const { fetching, messages, normalizedLocale } = this.state;
const { locale } = this.props;
const shouldFetch = (!fetching && _.isEmpty(messages)) || ((locale !== prevProps.locale) && (normalizedLocale && (locale !== normalizedLocale)));
if (shouldFetch) 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
}
render() {
const { fetching, normalizedLocale, messages } = this.state;
const { children } = this.props;
2021-02-13 00:55:39 +08:00
return (
<>
{(fetching || !normalizedLocale) && <LoadingScreen />}
{normalizedLocale
&& (
<IntlProvider locale={normalizedLocale} messages={messages}>
{children}
</IntlProvider>
)
}
</>
2017-03-11 02:33:46 +08:00
);
}
2017-06-03 03:25:02 +08:00
}
2017-03-11 02:33:46 +08:00
const IntlStartupContainer = withTracker(() => {
const { locale } = Settings.application;
const overrideLocaleFromPassedParameter = getFromUserSettings('bbb_override_default_locale', null);
return {
locale,
overrideLocaleFromPassedParameter,
};
})(IntlStartup);
export default IntlStartupContainer;
2017-03-11 02:33:46 +08:00
IntlStartup.propTypes = propTypes;
IntlStartup.defaultProps = defaultProps;