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';
|
2022-02-15 22:55:08 +08:00
|
|
|
import LoadingScreen from '/imports/ui/components/common/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';
|
2021-06-04 21:44:50 +08:00
|
|
|
import { Session } from 'meteor/session';
|
2021-08-26 00:12:38 +08:00
|
|
|
import Logger from '/imports/startup/client/logger';
|
2022-09-09 01:25:29 +08:00
|
|
|
import { formatLocaleCode } from '/imports/utils/string-utils';
|
2022-09-29 00:48:27 +08:00
|
|
|
import Intl from '/imports/ui/services/locale';
|
2017-03-11 02:33:46 +08:00
|
|
|
|
|
|
|
const propTypes = {
|
2018-08-03 03:23:11 +08:00
|
|
|
locale: PropTypes.string,
|
2021-08-02 22:24:35 +08:00
|
|
|
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;
|
2022-01-13 21:14:24 +08:00
|
|
|
const CLIENT_VERSION = Meteor.settings.public.app.html5ClientBuild;
|
2022-08-30 04:25:43 +08:00
|
|
|
const FALLBACK_ON_EMPTY_STRING = Meteor.settings.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'];
|
2021-06-04 21:44:50 +08:00
|
|
|
const LARGE_FONT_LANGUAGES = ['te', 'km'];
|
2019-05-14 21:15:54 +08:00
|
|
|
|
2017-03-11 02:33:46 +08:00
|
|
|
const defaultProps = {
|
2017-11-01 01:13:44 +08:00
|
|
|
locale: DEFAULT_LANGUAGE,
|
2021-08-02 22:24:35 +08:00
|
|
|
overrideLocaleFromPassedParameter: null,
|
2017-03-11 02:33:46 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class IntlStartup extends Component {
|
|
|
|
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-02-20 22:47:46 +08:00
|
|
|
const { fetching, messages, normalizedLocale } = this.state;
|
2021-08-02 22:24:35 +08:00
|
|
|
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-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}`;
|
2021-03-11 19:42:41 +08:00
|
|
|
const localesPath = 'locales';
|
2019-08-17 05:38:17 +08:00
|
|
|
|
2022-09-29 00:48:27 +08:00
|
|
|
Intl.fetching = true;
|
2018-08-03 03:23:11 +08:00
|
|
|
this.setState({ fetching: true }, () => {
|
|
|
|
fetch(url)
|
|
|
|
.then((response) => {
|
|
|
|
if (!response.ok) {
|
2021-03-23 21:09:28 +08:00
|
|
|
return false;
|
2018-08-03 03:23:11 +08:00
|
|
|
}
|
|
|
|
return response.json();
|
|
|
|
})
|
2021-03-11 19:42:41 +08:00
|
|
|
.then(({ normalizedLocale, regionDefaultLocale }) => {
|
2021-03-23 21:09:28 +08:00
|
|
|
const fetchFallbackMessages = new Promise((resolve, reject) => {
|
2022-01-13 21:14:24 +08:00
|
|
|
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);
|
|
|
|
}
|
2022-01-13 21:14:24 +08:00
|
|
|
fetch(`${localesPath}/${regionDefaultLocale}.json?v=${CLIENT_VERSION}`)
|
2021-03-23 21:09:28 +08:00
|
|
|
.then((response) => {
|
|
|
|
if (!response.ok) {
|
|
|
|
return resolve(false);
|
|
|
|
}
|
2021-08-26 00:12:38 +08:00
|
|
|
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);
|
|
|
|
}
|
2022-01-13 21:14:24 +08:00
|
|
|
fetch(`${localesPath}/${normalizedLocale}.json?v=${CLIENT_VERSION}`)
|
2021-03-23 21:09:28 +08:00
|
|
|
.then((response) => {
|
|
|
|
if (!response.ok) {
|
|
|
|
return resolve(false);
|
|
|
|
}
|
2021-08-26 00:12:38 +08:00
|
|
|
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-11 19:42:41 +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]);
|
|
|
|
}
|
2021-03-11 19:42:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const dasherizedLocale = normalizedLocale.replace('_', '-');
|
2022-09-09 01:25:29 +08:00
|
|
|
const { language, formattedLocale } = formatLocaleCode(dasherizedLocale);
|
2022-09-29 21:49:17 +08:00
|
|
|
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}`);
|
2021-08-26 01:06:57 +08:00
|
|
|
Settings.save();
|
2021-03-11 19:42:41 +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;
|
2022-09-09 01:25:29 +08:00
|
|
|
const { formattedLocale } = formatLocaleCode(normalizedLocale);
|
2020-07-16 03:52:34 +08:00
|
|
|
|
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}>
|
2021-06-03 22:42:02 +08:00
|
|
|
{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
|
|
|
|
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;
|