2017-06-04 10:40:14 +08:00
|
|
|
import React, { Component } from 'react';
|
2020-05-12 21:31:00 +08:00
|
|
|
import { withTracker } from 'meteor/react-meteor-data';
|
2017-06-04 10:40:14 +08:00
|
|
|
import PropTypes from 'prop-types';
|
2020-05-26 04:00:13 +08:00
|
|
|
import { IntlProvider } from 'react-intl';
|
2017-11-01 01:13:44 +08:00
|
|
|
import Settings from '/imports/ui/services/settings';
|
2019-08-17 05:38:17 +08:00
|
|
|
import LoadingScreen from '/imports/ui/components/loading-screen/component';
|
2020-09-09 02:54:08 +08:00
|
|
|
import getFromUserSettings from '/imports/ui/services/users-settings';
|
2021-01-20 00:02:20 +08:00
|
|
|
import _ from 'lodash';
|
2017-03-11 02:33:46 +08:00
|
|
|
|
|
|
|
const propTypes = {
|
2018-08-03 03:23:11 +08:00
|
|
|
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
|
|
|
|
2019-05-14 21:15:54 +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 {
|
2020-08-08 04:46:04 +08:00
|
|
|
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: {},
|
2018-08-03 03:23:11 +08:00
|
|
|
normalizedLocale: null,
|
2020-08-08 04:46:04 +08:00
|
|
|
fetching: true,
|
2017-03-11 02:33:46 +08:00
|
|
|
};
|
2017-05-18 00:02:13 +08:00
|
|
|
|
2020-02-12 00:59:04 +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
|
|
|
|
2020-08-08 04:46:04 +08:00
|
|
|
componentDidMount() {
|
2021-01-20 00:02:20 +08:00
|
|
|
const { locale, overrideLocaleFromPassedParameter } = this.props;
|
|
|
|
this.fetchLocalizedMessages(overrideLocaleFromPassedParameter || locale, true);
|
2017-06-06 20:33:53 +08:00
|
|
|
}
|
|
|
|
|
2020-08-25 20:29:23 +08:00
|
|
|
componentDidUpdate(prevProps) {
|
2021-01-20 00:02:20 +08:00
|
|
|
const { fetching, messages } = this.state;
|
|
|
|
const { locale } = this.props;
|
|
|
|
const shouldFetch = (!fetching && _.isEmpty(messages)) || (locale !== prevProps.locale);
|
|
|
|
if (shouldFetch) this.fetchLocalizedMessages(locale);
|
2017-06-06 20:33:53 +08:00
|
|
|
}
|
2017-03-11 02:33:46 +08:00
|
|
|
|
2020-03-11 21:55:41 +08:00
|
|
|
fetchLocalizedMessages(locale, init = false) {
|
2020-11-19 04:31:36 +08:00
|
|
|
const url = `./locale?locale=${locale}&init=${init}`;
|
2019-08-17 05:38:17 +08:00
|
|
|
|
2018-08-03 03:23:11 +08:00
|
|
|
this.setState({ fetching: true }, () => {
|
|
|
|
fetch(url)
|
|
|
|
.then((response) => {
|
|
|
|
if (!response.ok) {
|
|
|
|
return Promise.reject();
|
|
|
|
}
|
2017-03-11 02:33:46 +08:00
|
|
|
|
2018-08-03 03:23:11 +08:00
|
|
|
return response.json();
|
|
|
|
})
|
|
|
|
.then(({ messages, normalizedLocale }) => {
|
|
|
|
const dasherizedLocale = normalizedLocale.replace('_', '-');
|
|
|
|
this.setState({ messages, fetching: false, normalizedLocale: dasherizedLocale }, () => {
|
2020-08-08 04:46:04 +08:00
|
|
|
IntlStartup.saveLocale(dasherizedLocale);
|
2018-08-03 03:23:11 +08:00
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
this.setState({ fetching: false, normalizedLocale: null }, () => {
|
2020-08-08 04:46:04 +08:00
|
|
|
IntlStartup.saveLocale(DEFAULT_LANGUAGE);
|
2018-08-03 03:23:11 +08:00
|
|
|
});
|
2017-11-01 01:13:44 +08:00
|
|
|
});
|
2018-08-03 03:23:11 +08:00
|
|
|
});
|
2017-03-11 02:33:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2018-12-22 00:46:55 +08:00
|
|
|
const { fetching, normalizedLocale, messages } = this.state;
|
|
|
|
const { children } = this.props;
|
2020-07-16 03:52:34 +08:00
|
|
|
|
2020-08-08 04:46:04 +08:00
|
|
|
return (fetching || !normalizedLocale) ? <LoadingScreen /> : (
|
2020-07-16 03:52:34 +08:00
|
|
|
<IntlProvider locale={normalizedLocale} messages={messages}>
|
2018-12-22 00:46:55 +08:00
|
|
|
{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
|
|
|
|
2020-05-12 21:31:00 +08:00
|
|
|
const IntlStartupContainer = withTracker(() => {
|
2021-01-20 00:02:20 +08:00
|
|
|
const { locale } = Settings.application;
|
|
|
|
const overrideLocaleFromPassedParameter = getFromUserSettings('bbb_override_default_locale', null);
|
2020-05-12 21:31:00 +08:00
|
|
|
return {
|
|
|
|
locale,
|
2021-01-20 00:02:20 +08:00
|
|
|
overrideLocaleFromPassedParameter,
|
2020-05-12 21:31:00 +08:00
|
|
|
};
|
|
|
|
})(IntlStartup);
|
|
|
|
|
|
|
|
export default IntlStartupContainer;
|
2017-03-11 02:33:46 +08:00
|
|
|
|
|
|
|
IntlStartup.propTypes = propTypes;
|
|
|
|
IntlStartup.defaultProps = defaultProps;
|