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

189 lines
6.6 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';
import { Session } from 'meteor/session';
import Logger from '/imports/startup/client/logger';
2017-03-11 02:33:46 +08:00
const propTypes = {
locale: PropTypes.string,
overrideLocaleFromPassedParameter: 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;
const CLIENT_VERSION = Meteor.settings.public.app.html5ClientBuild;
2017-06-06 20:43:57 +08:00
2021-06-15 00:33:17 +08:00
const RTL_LANGUAGES = ['ar', 'dv', 'fa', 'he'];
const LARGE_FONT_LANGUAGES = ['te', 'km'];
2017-03-11 02:33:46 +08:00
const defaultProps = {
2017-11-01 01:13:44 +08:00
locale: DEFAULT_LANGUAGE,
overrideLocaleFromPassedParameter: null,
2017-03-11 02:33:46 +08:00
};
class IntlStartup extends Component {
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, overrideLocaleFromPassedParameter } = this.props;
if (overrideLocaleFromPassedParameter !== prevProps.overrideLocaleFromPassedParameter) {
this.fetchLocalizedMessages(overrideLocaleFromPassedParameter);
} else {
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}`;
const localesPath = 'locales';
this.setState({ fetching: true }, () => {
fetch(url)
.then((response) => {
if (!response.ok) {
2021-03-23 21:09:28 +08:00
return false;
}
return response.json();
})
.then(({ normalizedLocale, regionDefaultLocale }) => {
2021-03-23 21:09:28 +08:00
const fetchFallbackMessages = new Promise((resolve, reject) => {
fetch(`${localesPath}/${DEFAULT_LANGUAGE}.json?v=${CLIENT_VERSION}`)
2021-03-23 21:09:28 +08:00
.then((response) => {
if (!response.ok) {
return reject();
}
return resolve(response.json());
});
});
const fetchRegionMessages = new Promise((resolve) => {
if (!regionDefaultLocale) {
return resolve(false);
}
fetch(`${localesPath}/${regionDefaultLocale}.json?v=${CLIENT_VERSION}`)
2021-03-23 21:09:28 +08:00
.then((response) => {
if (!response.ok) {
return resolve(false);
}
return response.json()
.then((jsonResponse) => resolve(jsonResponse))
.catch(() => {
Logger.error({ logCode: 'intl_parse_locale_SyntaxError' }, `Could not parse locale file ${regionDefaultLocale}.json, invalid json`);
resolve(false);
});
2021-03-23 21:09:28 +08:00
});
});
const fetchSpecificMessages = new Promise((resolve) => {
if (!normalizedLocale || normalizedLocale === DEFAULT_LANGUAGE || normalizedLocale === regionDefaultLocale) {
return resolve(false);
}
fetch(`${localesPath}/${normalizedLocale}.json?v=${CLIENT_VERSION}`)
2021-03-23 21:09:28 +08:00
.then((response) => {
if (!response.ok) {
return resolve(false);
}
return response.json()
.then((jsonResponse) => resolve(jsonResponse))
.catch(() => {
Logger.error({ logCode: 'intl_parse_locale_SyntaxError' }, `Could not parse locale file ${normalizedLocale}.json, invalid json`);
resolve(false);
});
2021-03-23 21:09:28 +08:00
});
});
2021-03-23 21:09:28 +08:00
Promise.all([fetchFallbackMessages, fetchRegionMessages, fetchSpecificMessages])
.then((values) => {
let mergedMessages = Object.assign({}, values[0]);
if (!values[1] && !values[2]) {
normalizedLocale = DEFAULT_LANGUAGE;
} else {
if (values[1]) {
mergedMessages = Object.assign(mergedMessages, values[1]);
}
if (values[2]) {
mergedMessages = Object.assign(mergedMessages, values[2]);
}
}
const dasherizedLocale = normalizedLocale.replace('_', '-');
2021-03-23 21:09:28 +08:00
this.setState({ messages: mergedMessages, fetching: false, normalizedLocale: dasherizedLocale }, () => {
2021-08-26 01:06:57 +08:00
Settings.application.locale = dasherizedLocale;
if (RTL_LANGUAGES.includes(dasherizedLocale.substring(0, 2))) {
document.body.parentNode.setAttribute('dir', 'rtl');
Settings.application.isRTL = true;
} else {
document.body.parentNode.setAttribute('dir', 'ltr');
Settings.application.isRTL = false;
}
Session.set('isLargeFont', LARGE_FONT_LANGUAGES.includes(dasherizedLocale.substring(0, 2)));
window.dispatchEvent(new Event('localeChanged'));
Settings.save();
});
});
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>
2021-02-13 00:55:39 +08:00
)
}
</>
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;