2021-08-06 04:30:08 +08:00
|
|
|
import React from 'react';
|
|
|
|
import ReactDOM from 'react-dom';
|
|
|
|
import './index.css';
|
|
|
|
import { IntlProvider } from 'react-intl';
|
|
|
|
import App from './App';
|
2022-02-14 23:30:01 +08:00
|
|
|
import { UserDetailsProvider } from './components/UserDetails/context';
|
2021-08-06 04:30:08 +08:00
|
|
|
|
2021-09-15 01:18:45 +08:00
|
|
|
const RTL_LANGUAGES = ['ar', 'dv', 'fa', 'he'];
|
|
|
|
|
2024-10-23 04:00:03 +08:00
|
|
|
function isValidLocale(locale) {
|
|
|
|
try {
|
|
|
|
const intl = new Intl.Locale(locale);
|
|
|
|
return !!intl;
|
|
|
|
} catch (error) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function normalizeLocale(lang) {
|
|
|
|
const lParts = lang.split('-');
|
|
|
|
|
|
|
|
// 'pt' returns 'pt'
|
|
|
|
// 'pt-br' and 'pt-BR' return 'pt_BR'
|
|
|
|
// 'pt_BR' returns 'pt_BR'
|
|
|
|
return lParts.length > 1 ? `${lParts[0]}_${lParts[1].toUpperCase()}` : lang;
|
|
|
|
}
|
|
|
|
|
2021-08-21 02:26:55 +08:00
|
|
|
function getLanguage() {
|
|
|
|
let { language } = navigator;
|
|
|
|
|
|
|
|
const urlSearchParams = new URLSearchParams(window.location.search);
|
|
|
|
const params = Object.fromEntries(urlSearchParams.entries());
|
|
|
|
if (typeof params.lang !== 'undefined') {
|
|
|
|
language = params.lang;
|
|
|
|
}
|
|
|
|
|
|
|
|
return language;
|
|
|
|
}
|
|
|
|
|
2021-08-06 04:30:08 +08:00
|
|
|
class Dashboard extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
intlMessages: {},
|
2021-08-21 02:26:55 +08:00
|
|
|
intlLocale: getLanguage(),
|
2021-08-06 04:30:08 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
this.setMessages();
|
2021-09-15 01:18:45 +08:00
|
|
|
this.setRtl();
|
2021-08-06 04:30:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
setMessages() {
|
|
|
|
const fetchMessages = (lang) => new Promise((resolve, reject) => {
|
2024-10-23 04:00:03 +08:00
|
|
|
const url = `/html5client/locales/${normalizeLocale(lang)}.json`;
|
2021-08-06 04:30:08 +08:00
|
|
|
fetch(url).then((response) => {
|
|
|
|
if (!response.ok) return reject();
|
|
|
|
return resolve(response.json());
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-08-21 02:26:55 +08:00
|
|
|
Promise.all([fetchMessages('en'), fetchMessages(getLanguage())])
|
2021-08-06 04:30:08 +08:00
|
|
|
.then((values) => {
|
2021-08-07 06:58:48 +08:00
|
|
|
let mergedMessages = {};
|
|
|
|
|
|
|
|
if (values[0]) {
|
|
|
|
mergedMessages = Object.assign(mergedMessages, values[0]);
|
|
|
|
}
|
2021-08-06 04:30:08 +08:00
|
|
|
|
|
|
|
if (values[1]) {
|
|
|
|
mergedMessages = Object.assign(mergedMessages, values[1]);
|
|
|
|
}
|
|
|
|
|
2021-08-21 02:26:55 +08:00
|
|
|
this.setState({ intlMessages: mergedMessages });
|
2021-08-06 04:30:08 +08:00
|
|
|
}).catch(() => {});
|
|
|
|
}
|
|
|
|
|
2021-09-15 01:18:45 +08:00
|
|
|
setRtl() {
|
|
|
|
const { intlLocale } = this.state;
|
|
|
|
|
2021-10-15 04:21:57 +08:00
|
|
|
if (RTL_LANGUAGES.includes(intlLocale.substring(0, 2))) {
|
2021-09-15 01:18:45 +08:00
|
|
|
document.body.parentNode.setAttribute('dir', 'rtl');
|
2022-02-14 23:30:01 +08:00
|
|
|
} else {
|
|
|
|
document.body.parentNode.setAttribute('dir', 'ltr');
|
2021-09-15 01:18:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-06 04:30:08 +08:00
|
|
|
render() {
|
|
|
|
const { intlLocale, intlMessages } = this.state;
|
|
|
|
|
2024-10-23 04:00:03 +08:00
|
|
|
const locale = isValidLocale(intlLocale) ? intlLocale : undefined;
|
|
|
|
|
2021-08-06 04:30:08 +08:00
|
|
|
return (
|
2022-02-14 23:30:01 +08:00
|
|
|
<UserDetailsProvider>
|
2024-10-23 04:00:03 +08:00
|
|
|
<IntlProvider defaultLocale="en" locale={locale} messages={intlMessages}>
|
2022-02-14 23:30:01 +08:00
|
|
|
<App />
|
|
|
|
</IntlProvider>
|
|
|
|
</UserDetailsProvider>
|
2021-08-06 04:30:08 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const rootElement = document.getElementById('root');
|
|
|
|
ReactDOM.render(<Dashboard />, rootElement);
|