2017-06-04 10:40:14 +08:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2018-12-22 00:46:55 +08:00
|
|
|
import { IntlProvider, addLocaleData } 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';
|
2017-03-11 02:33:46 +08:00
|
|
|
|
2018-12-22 00:46:55 +08:00
|
|
|
// currently supported locales.
|
2019-04-03 23:50:01 +08:00
|
|
|
import ar from 'react-intl/locale-data/ar';
|
2019-01-30 05:38:40 +08:00
|
|
|
import bg from 'react-intl/locale-data/bg';
|
2019-02-22 23:06:19 +08:00
|
|
|
import cs from 'react-intl/locale-data/cs';
|
2020-01-18 04:26:18 +08:00
|
|
|
import da from 'react-intl/locale-data/da';
|
2018-12-22 00:46:55 +08:00
|
|
|
import de from 'react-intl/locale-data/de';
|
2019-01-30 05:38:40 +08:00
|
|
|
import el from 'react-intl/locale-data/el';
|
|
|
|
import en from 'react-intl/locale-data/en';
|
2018-12-22 00:46:55 +08:00
|
|
|
import es from 'react-intl/locale-data/es';
|
2019-04-24 21:08:11 +08:00
|
|
|
import eu from 'react-intl/locale-data/eu';
|
2019-01-30 05:38:40 +08:00
|
|
|
import fa from 'react-intl/locale-data/fa';
|
2019-06-27 02:15:55 +08:00
|
|
|
import fi from 'react-intl/locale-data/fi';
|
2019-01-30 05:38:40 +08:00
|
|
|
import fr from 'react-intl/locale-data/fr';
|
2020-01-18 04:26:18 +08:00
|
|
|
import gl from 'react-intl/locale-data/gl';
|
2019-03-06 03:31:28 +08:00
|
|
|
import he from 'react-intl/locale-data/he';
|
2019-04-24 21:08:11 +08:00
|
|
|
import hi from 'react-intl/locale-data/hi';
|
2019-06-04 03:44:03 +08:00
|
|
|
import hu from 'react-intl/locale-data/hu';
|
2018-12-22 00:46:55 +08:00
|
|
|
import id from 'react-intl/locale-data/id';
|
|
|
|
import it from 'react-intl/locale-data/it';
|
2019-01-30 05:38:40 +08:00
|
|
|
import ja from 'react-intl/locale-data/ja';
|
|
|
|
import km from 'react-intl/locale-data/km';
|
2019-11-16 06:13:43 +08:00
|
|
|
import ko from 'react-intl/locale-data/ko';
|
|
|
|
import nl from 'react-intl/locale-data/nl';
|
2019-01-30 05:15:10 +08:00
|
|
|
import pl from 'react-intl/locale-data/pl';
|
2019-01-30 05:38:40 +08:00
|
|
|
import pt from 'react-intl/locale-data/pt';
|
2019-11-06 02:05:41 +08:00
|
|
|
import ro from 'react-intl/locale-data/ro';
|
2019-01-30 05:38:40 +08:00
|
|
|
import ru from 'react-intl/locale-data/ru';
|
2019-11-16 06:13:43 +08:00
|
|
|
import sk from 'react-intl/locale-data/sk';
|
|
|
|
import sr from 'react-intl/locale-data/sr';
|
2019-05-09 22:47:47 +08:00
|
|
|
import sv from 'react-intl/locale-data/sv';
|
2020-02-15 04:45:08 +08:00
|
|
|
import th from 'react-intl/locale-data/th';
|
2019-01-30 05:38:40 +08:00
|
|
|
import tr from 'react-intl/locale-data/tr';
|
|
|
|
import uk from 'react-intl/locale-data/uk';
|
2019-05-24 04:53:44 +08:00
|
|
|
import vi from 'react-intl/locale-data/vi';
|
2019-01-30 05:38:40 +08:00
|
|
|
import zh from 'react-intl/locale-data/zh';
|
2019-01-10 05:43:54 +08:00
|
|
|
|
2018-12-22 00:46:55 +08:00
|
|
|
|
|
|
|
addLocaleData([
|
2019-04-03 23:50:01 +08:00
|
|
|
...ar,
|
2019-01-30 05:38:40 +08:00
|
|
|
...bg,
|
2019-02-22 23:06:19 +08:00
|
|
|
...cs,
|
2020-01-18 04:26:18 +08:00
|
|
|
...da,
|
2018-12-22 00:46:55 +08:00
|
|
|
...de,
|
2019-01-30 05:38:40 +08:00
|
|
|
...el,
|
|
|
|
...en,
|
2018-12-22 00:46:55 +08:00
|
|
|
...es,
|
2019-04-24 21:08:11 +08:00
|
|
|
...eu,
|
2019-01-30 05:38:40 +08:00
|
|
|
...fa,
|
2019-06-27 02:15:55 +08:00
|
|
|
...fi,
|
2019-01-30 05:38:40 +08:00
|
|
|
...fr,
|
2020-01-18 04:26:18 +08:00
|
|
|
...gl,
|
2019-03-06 03:31:28 +08:00
|
|
|
...he,
|
2019-04-24 21:08:11 +08:00
|
|
|
...hi,
|
2019-06-04 03:44:03 +08:00
|
|
|
...hu,
|
2018-12-22 00:46:55 +08:00
|
|
|
...id,
|
|
|
|
...it,
|
2019-01-30 05:38:40 +08:00
|
|
|
...ja,
|
|
|
|
...km,
|
2019-11-16 06:13:43 +08:00
|
|
|
...ko,
|
|
|
|
...nl,
|
2019-01-30 05:15:10 +08:00
|
|
|
...pl,
|
2019-01-30 05:38:40 +08:00
|
|
|
...pt,
|
2019-11-06 02:05:41 +08:00
|
|
|
...ro,
|
2019-01-30 05:38:40 +08:00
|
|
|
...ru,
|
2019-11-16 06:13:43 +08:00
|
|
|
...sk,
|
|
|
|
...sr,
|
2019-05-09 22:47:47 +08:00
|
|
|
...sv,
|
2020-02-15 04:45:08 +08:00
|
|
|
...th,
|
2019-01-30 05:38:40 +08:00
|
|
|
...tr,
|
|
|
|
...uk,
|
2019-05-24 04:53:44 +08:00
|
|
|
...vi,
|
2019-01-30 05:38:40 +08:00
|
|
|
...zh,
|
2018-12-22 00:46:55 +08:00
|
|
|
]);
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2017-10-27 21:23:18 +08:00
|
|
|
const DEFAULT_LANGUAGE = Meteor.settings.public.app.defaultSettings.application.locale;
|
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 {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
messages: {},
|
2018-08-03 03:23:11 +08:00
|
|
|
normalizedLocale: null,
|
|
|
|
fetching: false,
|
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
|
|
|
|
2017-06-06 20:33:53 +08:00
|
|
|
componentWillMount() {
|
2018-12-22 00:46:55 +08:00
|
|
|
const { locale } = this.props;
|
|
|
|
this.fetchLocalizedMessages(locale);
|
2017-06-06 20:33:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUpdate(nextProps) {
|
2018-12-22 00:46:55 +08:00
|
|
|
const { fetching, normalizedLocale } = this.state;
|
|
|
|
const { locale } = nextProps;
|
|
|
|
|
|
|
|
if (!fetching
|
|
|
|
&& normalizedLocale
|
|
|
|
&& locale.toLowerCase() !== normalizedLocale.toLowerCase()) {
|
|
|
|
this.fetchLocalizedMessages(locale);
|
2017-06-06 20:33:53 +08:00
|
|
|
}
|
|
|
|
}
|
2017-03-11 02:33:46 +08:00
|
|
|
|
|
|
|
fetchLocalizedMessages(locale) {
|
|
|
|
const url = `/html5client/locale?locale=${locale}`;
|
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 }, () => {
|
2019-05-14 21:15:54 +08:00
|
|
|
this.saveLocale(dasherizedLocale);
|
2018-08-03 03:23:11 +08:00
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
this.setState({ fetching: false, normalizedLocale: null }, () => {
|
2019-05-14 21:15:54 +08:00
|
|
|
this.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
|
|
|
}
|
|
|
|
|
2019-05-14 21:15:54 +08:00
|
|
|
saveLocale(localeName) {
|
|
|
|
Settings.application.locale = localeName;
|
2019-05-24 04:53:44 +08:00
|
|
|
if (RTL_LANGUAGES.includes(localeName.substring(0, 2))) {
|
2019-05-14 21:15:54 +08:00
|
|
|
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
|
|
|
render() {
|
2018-12-22 00:46:55 +08:00
|
|
|
const { fetching, normalizedLocale, messages } = this.state;
|
|
|
|
const { children } = this.props;
|
|
|
|
|
2019-08-17 05:38:17 +08:00
|
|
|
return fetching ? <LoadingScreen /> : (
|
2018-12-22 00:46:55 +08:00
|
|
|
<IntlProvider locale={normalizedLocale} messages={messages}>
|
|
|
|
{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
|
|
|
|
2019-08-17 05:38:17 +08:00
|
|
|
export default IntlStartup;
|
2017-03-11 02:33:46 +08:00
|
|
|
|
|
|
|
IntlStartup.propTypes = propTypes;
|
|
|
|
IntlStartup.defaultProps = defaultProps;
|