2024-03-07 01:28:18 +08:00
|
|
|
import React, { useContext, useEffect } from 'react';
|
|
|
|
import { useIntl } from 'react-intl';
|
2024-05-02 03:57:08 +08:00
|
|
|
import * as PluginSdk from 'bigbluebutton-html-plugin-sdk';
|
2024-05-29 21:26:11 +08:00
|
|
|
import { getSettingsSingletonInstance } from '/imports/ui/services/settings';
|
2024-06-06 21:50:03 +08:00
|
|
|
import Session from '/imports/ui/services/storage/in-memory';
|
2024-03-07 01:28:18 +08:00
|
|
|
import { formatLocaleCode } from '/imports/utils/string-utils';
|
|
|
|
import useCurrentLocale from '/imports/ui/core/local-states/useCurrentLocale';
|
|
|
|
import { LoadingContext } from '/imports/ui/components/common/loading-screen/loading-screen-HOC/component';
|
2024-05-02 03:57:08 +08:00
|
|
|
import { UI_DATA_LISTENER_SUBSCRIBED } from 'bigbluebutton-html-plugin-sdk/dist/cjs/ui-data-hooks/consts';
|
2024-05-20 23:02:32 +08:00
|
|
|
import intlHolder from '/imports/ui/core/singletons/intlHolder';
|
2024-05-31 01:07:02 +08:00
|
|
|
import useUserChangedLocalSettings from '/imports/ui/services/settings/hooks/useUserChangedLocalSettings';
|
2024-06-20 00:24:09 +08:00
|
|
|
import { localUserSettings } from '/imports/ui/core/local-states/useUserSettings';
|
2024-06-27 00:40:53 +08:00
|
|
|
import { layoutDispatch } from '/imports/ui/components/layout/context';
|
|
|
|
import { ACTIONS } from '/imports/ui/components/layout/enums';
|
2024-03-07 01:28:18 +08:00
|
|
|
|
|
|
|
const RTL_LANGUAGES = ['ar', 'dv', 'fa', 'he'];
|
|
|
|
const LARGE_FONT_LANGUAGES = ['te', 'km'];
|
|
|
|
|
|
|
|
interface IntlAdapterProps {
|
|
|
|
children: React.ReactNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
const IntlAdapter: React.FC<IntlAdapterProps> = ({
|
|
|
|
children,
|
|
|
|
}) => {
|
2024-05-29 21:26:11 +08:00
|
|
|
const Settings = getSettingsSingletonInstance();
|
2024-05-09 22:45:50 +08:00
|
|
|
const [currentLocale, setCurrentLocale] = useCurrentLocale();
|
2024-03-07 01:28:18 +08:00
|
|
|
const intl = useIntl();
|
|
|
|
const loadingContextInfo = useContext(LoadingContext);
|
2024-05-31 01:07:02 +08:00
|
|
|
const setLocalSettings = useUserChangedLocalSettings();
|
2024-06-27 00:40:53 +08:00
|
|
|
const layoutContextDispatch = layoutDispatch();
|
2024-05-20 23:02:32 +08:00
|
|
|
|
2024-05-29 21:26:11 +08:00
|
|
|
const DEFAULT_LANGUAGE = window.meetingClientSettings.public.app.defaultSettings.application.fallbackLocale;
|
|
|
|
|
2024-05-20 23:02:32 +08:00
|
|
|
useEffect(() => {
|
|
|
|
intlHolder.setIntl(intl);
|
2024-07-30 03:17:35 +08:00
|
|
|
}, [currentLocale]);
|
2024-05-20 23:02:32 +08:00
|
|
|
|
2024-05-02 03:57:08 +08:00
|
|
|
const sendUiDataToPlugins = () => {
|
|
|
|
window.dispatchEvent(new CustomEvent(PluginSdk.IntlLocaleUiDataNames.CURRENT_LOCALE, {
|
|
|
|
detail: {
|
|
|
|
locale: currentLocale,
|
|
|
|
fallbackLocale: DEFAULT_LANGUAGE,
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
};
|
2024-03-07 01:28:18 +08:00
|
|
|
const setUp = () => {
|
|
|
|
if (currentLocale) {
|
|
|
|
const { language, formattedLocale } = formatLocaleCode(currentLocale);
|
|
|
|
// @ts-ignore - JS code
|
|
|
|
Settings.application.locale = currentLocale;
|
|
|
|
if (RTL_LANGUAGES.includes(currentLocale.substring(0, 2))) {
|
|
|
|
// @ts-ignore - JS code
|
|
|
|
document.body.parentNode.setAttribute('dir', 'rtl');
|
|
|
|
// @ts-ignore - JS code
|
|
|
|
Settings.application.isRTL = true;
|
2024-06-27 00:40:53 +08:00
|
|
|
layoutContextDispatch({
|
|
|
|
type: ACTIONS.SET_IS_RTL,
|
|
|
|
value: true,
|
|
|
|
});
|
2024-03-07 01:28:18 +08:00
|
|
|
} else {
|
|
|
|
// @ts-ignore - JS code
|
|
|
|
document.body.parentNode.setAttribute('dir', 'ltr');
|
|
|
|
// @ts-ignore - JS code
|
|
|
|
Settings.application.isRTL = false;
|
2024-06-27 00:40:53 +08:00
|
|
|
layoutContextDispatch({
|
|
|
|
type: ACTIONS.SET_IS_RTL,
|
|
|
|
value: false,
|
|
|
|
});
|
2024-03-07 01:28:18 +08:00
|
|
|
}
|
2024-06-06 21:50:03 +08:00
|
|
|
Session.setItem('isLargeFont', LARGE_FONT_LANGUAGES.includes(currentLocale.substring(0, 2)));
|
2024-03-07 01:28:18 +08:00
|
|
|
document.getElementsByTagName('html')[0].lang = formattedLocale;
|
|
|
|
document.body.classList.add(`lang-${language}`);
|
2024-05-31 01:07:02 +08:00
|
|
|
Settings.save(setLocalSettings);
|
2024-03-07 01:28:18 +08:00
|
|
|
}
|
|
|
|
};
|
2024-05-02 03:57:08 +08:00
|
|
|
const runOnMountAndUnmount = () => {
|
|
|
|
window.addEventListener(
|
|
|
|
`${UI_DATA_LISTENER_SUBSCRIBED}-${PluginSdk.IntlLocaleUiDataNames.CURRENT_LOCALE}`,
|
|
|
|
sendUiDataToPlugins,
|
|
|
|
);
|
2024-05-09 22:45:50 +08:00
|
|
|
// @ts-ignore - JS code
|
|
|
|
const { locale } = Settings.application;
|
2024-07-16 21:40:13 +08:00
|
|
|
const clientSettings = window.meetingClientSettings.public.app.defaultSettings.application;
|
2024-06-11 04:45:15 +08:00
|
|
|
const { overrideLocale } = clientSettings;
|
2024-06-20 00:24:09 +08:00
|
|
|
const { bbb_override_default_locale } = localUserSettings();
|
|
|
|
if (typeof bbb_override_default_locale === 'string') {
|
|
|
|
setCurrentLocale(bbb_override_default_locale);
|
|
|
|
} else if (
|
2024-05-09 22:45:50 +08:00
|
|
|
typeof locale === 'string'
|
|
|
|
&& locale !== currentLocale
|
|
|
|
) {
|
2024-06-11 04:45:15 +08:00
|
|
|
setCurrentLocale(overrideLocale || locale);
|
2024-05-09 22:45:50 +08:00
|
|
|
} else {
|
|
|
|
setUp();
|
|
|
|
}
|
2024-05-02 03:57:08 +08:00
|
|
|
return () => {
|
|
|
|
window.removeEventListener(
|
|
|
|
`${UI_DATA_LISTENER_SUBSCRIBED}-${PluginSdk.IntlLocaleUiDataNames.CURRENT_LOCALE}`,
|
|
|
|
sendUiDataToPlugins,
|
|
|
|
);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const runOnCurrentLocaleUpdate = () => {
|
|
|
|
setUp();
|
|
|
|
sendUiDataToPlugins();
|
2024-08-07 01:53:46 +08:00
|
|
|
window.removeEventListener(
|
|
|
|
`${UI_DATA_LISTENER_SUBSCRIBED}-${PluginSdk.IntlLocaleUiDataNames.CURRENT_LOCALE}`,
|
|
|
|
sendUiDataToPlugins,
|
|
|
|
);
|
|
|
|
window.addEventListener(
|
|
|
|
`${UI_DATA_LISTENER_SUBSCRIBED}-${PluginSdk.IntlLocaleUiDataNames.CURRENT_LOCALE}`,
|
|
|
|
sendUiDataToPlugins,
|
|
|
|
);
|
2024-05-02 03:57:08 +08:00
|
|
|
};
|
2024-03-07 01:28:18 +08:00
|
|
|
|
2024-05-02 03:57:08 +08:00
|
|
|
useEffect(runOnMountAndUnmount, []);
|
|
|
|
useEffect(runOnCurrentLocaleUpdate, [currentLocale]);
|
2024-03-07 01:28:18 +08:00
|
|
|
return !loadingContextInfo.isLoading ? children : null;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default IntlAdapter;
|