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';
|
2018-08-03 03:23:11 +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-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';
|
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-01-30 05:38:40 +08:00
|
|
|
import fa from 'react-intl/locale-data/fa';
|
|
|
|
import fr from 'react-intl/locale-data/fr';
|
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-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';
|
|
|
|
import ru from 'react-intl/locale-data/ru';
|
|
|
|
import tr from 'react-intl/locale-data/tr';
|
|
|
|
import uk from 'react-intl/locale-data/uk';
|
|
|
|
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-01-30 05:38:40 +08:00
|
|
|
...bg,
|
2019-02-22 23:06:19 +08:00
|
|
|
...cs,
|
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-01-30 05:38:40 +08:00
|
|
|
...fa,
|
|
|
|
...fr,
|
2018-12-22 00:46:55 +08:00
|
|
|
...id,
|
|
|
|
...it,
|
2019-01-30 05:38:40 +08:00
|
|
|
...ja,
|
|
|
|
...km,
|
2019-01-30 05:15:10 +08:00
|
|
|
...pl,
|
2019-01-30 05:38:40 +08:00
|
|
|
...pt,
|
|
|
|
...ru,
|
|
|
|
...tr,
|
|
|
|
...uk,
|
|
|
|
...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
|
|
|
|
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
|
|
|
|
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}`;
|
|
|
|
|
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 }, () => {
|
|
|
|
Settings.application.locale = dasherizedLocale;
|
|
|
|
Settings.save();
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
this.setState({ fetching: false, normalizedLocale: null }, () => {
|
|
|
|
Settings.application.locale = DEFAULT_LANGUAGE;
|
|
|
|
Settings.save();
|
|
|
|
});
|
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;
|
|
|
|
|
|
|
|
return fetching ? <LoadingScreen /> : (
|
|
|
|
<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
|
|
|
|
|
|
|
export default IntlStartup;
|
|
|
|
|
|
|
|
IntlStartup.propTypes = propTypes;
|
|
|
|
IntlStartup.defaultProps = defaultProps;
|