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

234 lines
8.4 KiB
React
Raw Normal View History

import React, { Component } from 'react';
import { withTracker } from 'meteor/react-meteor-data';
import * as PluginSdk from 'bigbluebutton-html-plugin-sdk';
import { UI_DATA_LISTENER_SUBSCRIBED } from 'bigbluebutton-html-plugin-sdk/dist/cjs/ui-data-hooks/consts';
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/common/loading-screen/component';
import getFromUserSettings from '/imports/ui/services/users-settings';
import { Session } from 'meteor/session';
import Logger from '/imports/startup/client/logger';
2022-09-09 01:25:29 +08:00
import { formatLocaleCode } from '/imports/utils/string-utils';
import Intl from '/imports/ui/services/locale';
2023-02-23 21:27:16 +08:00
import { isEmpty } from 'radash';
2024-01-30 21:03:11 +08:00
import useUserChangedLocalSettings from '/imports/ui/services/settings/hooks/useUserChangedLocalSettings';
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
};
const DEFAULT_LANGUAGE = window.meetingClientSettings.public.app.defaultSettings.application.fallbackLocale;
const CLIENT_VERSION = window.meetingClientSettings.public.app.html5ClientBuild;
const FALLBACK_ON_EMPTY_STRING = window.meetingClientSettings.public.app.fallbackOnEmptyLocaleString;
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);
this.sendUiDataToPlugins = this.sendUiDataToPlugins.bind(this);
2017-03-11 02:33:46 +08:00
}
2018-12-06 01:42:31 +08:00
componentDidMount() {
const { locale, overrideLocaleFromPassedParameter } = this.props;
this.fetchLocalizedMessages(overrideLocaleFromPassedParameter || locale, true);
window.addEventListener(`${UI_DATA_LISTENER_SUBSCRIBED}-${PluginSdk.IntlLocaleUiDataNames.CURRENT_LOCALE}`, this.sendUiDataToPlugins);
}
2020-08-25 20:29:23 +08:00
componentDidUpdate(prevProps) {
const { fetching, messages, normalizedLocale } = this.state;
const { locale, overrideLocaleFromPassedParameter } = this.props;
this.sendUiDataToPlugins();
if (overrideLocaleFromPassedParameter !== prevProps.overrideLocaleFromPassedParameter) {
this.fetchLocalizedMessages(overrideLocaleFromPassedParameter);
} else {
2023-02-23 21:27:16 +08:00
const shouldFetch = (!fetching && isEmpty(messages)) || ((locale !== prevProps.locale) && (normalizedLocale && (locale !== normalizedLocale)));
if (shouldFetch) this.fetchLocalizedMessages(locale);
}
}
2017-03-11 02:33:46 +08:00
componentWillUnmount() {
window.removeEventListener(`${UI_DATA_LISTENER_SUBSCRIBED}-${PluginSdk.IntlLocaleUiDataNames.CURRENT_LOCALE}`, this.sendUiDataToPlugins);
}
sendUiDataToPlugins() {
const {
locale,
} = this.props;
window.dispatchEvent(new CustomEvent(PluginSdk.IntlLocaleUiDataNames.CURRENT_LOCALE, {
detail: {
locale,
fallbackLocale: DEFAULT_LANGUAGE,
},
}));
}
fetchLocalizedMessages(locale, init = false) {
const url = `./locale?locale=${locale}&init=${init}`;
const localesPath = 'locales';
2024-01-30 21:03:11 +08:00
const { setLocalSettings } = this.props;
Intl.fetching = true;
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('_', '-');
2022-09-09 01:25:29 +08:00
const { language, formattedLocale } = formatLocaleCode(dasherizedLocale);
Intl.setLocale(formattedLocale, mergedMessages);
2022-09-09 01:25:29 +08:00
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'));
2022-09-09 01:25:29 +08:00
document.getElementsByTagName('html')[0].lang = formattedLocale;
document.body.classList.add(`lang-${language}`);
2024-01-30 21:03:11 +08:00
Settings.save(setLocalSettings);
});
});
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;
2022-09-09 01:25:29 +08:00
const { formattedLocale } = formatLocaleCode(normalizedLocale);
2021-02-13 00:55:39 +08:00
return (
<>
{(fetching || !normalizedLocale) && <LoadingScreen />}
{normalizedLocale
&& (
2022-09-09 01:25:29 +08:00
<IntlProvider fallbackOnEmptyString={FALLBACK_ON_EMPTY_STRING} locale={formattedLocale} 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
2024-02-16 01:11:18 +08:00
const IntlStartupContainer = (props) => {
const setLocalSettings = useUserChangedLocalSettings();
return (
<IntlStartup
{...{
setLocalSettings,
...props,
}}
/>
);
};
export default withTracker(() => {
const { locale } = Settings.application;
const overrideLocaleFromPassedParameter = getFromUserSettings('bbb_override_default_locale', null);
2024-02-16 01:11:18 +08:00
return {
locale,
overrideLocaleFromPassedParameter,
};
2024-02-16 01:11:18 +08:00
})(IntlStartupContainer);
2017-03-11 02:33:46 +08:00
IntlStartup.propTypes = propTypes;
IntlStartup.defaultProps = defaultProps;