2021-01-30 00:17:35 +08:00
|
|
|
import React, { PureComponent } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2022-12-14 21:21:44 +08:00
|
|
|
import { injectIntl } from 'react-intl';
|
2021-01-30 00:17:35 +08:00
|
|
|
|
|
|
|
const DEFAULT_VALUE = 'select';
|
|
|
|
const DEFAULT_KEY = -1;
|
|
|
|
|
|
|
|
const propTypes = {
|
|
|
|
allLocales: PropTypes.arrayOf(PropTypes.object).isRequired,
|
|
|
|
value: PropTypes.string,
|
|
|
|
handleChange: PropTypes.func.isRequired,
|
|
|
|
elementId: PropTypes.string.isRequired,
|
|
|
|
selectMessage: PropTypes.string.isRequired,
|
|
|
|
};
|
|
|
|
|
|
|
|
const defaultProps = {
|
|
|
|
value: null,
|
|
|
|
};
|
|
|
|
|
|
|
|
class LocalesDropdown extends PureComponent {
|
|
|
|
// returns an array with the base language list + variations of currently selected language
|
|
|
|
filterLocaleVariations(value) {
|
|
|
|
const { allLocales } = this.props;
|
|
|
|
if (allLocales) {
|
|
|
|
if (Meteor.settings.public.app.showAllAvailableLocales) {
|
|
|
|
return allLocales;
|
|
|
|
}
|
|
|
|
|
|
|
|
// splits value if not empty
|
|
|
|
const splitValue = (value) ? value.split('-')[0] : '';
|
|
|
|
|
2021-02-03 02:44:58 +08:00
|
|
|
const allLocaleCodes = [];
|
2021-10-30 01:10:20 +08:00
|
|
|
allLocales.map((item) => allLocaleCodes.push(item.locale));
|
2021-02-03 02:44:58 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
locales show if:
|
|
|
|
1. it is a general version of a locale with no specific locales
|
|
|
|
2. it is a specific version of a selected locale with many specific versions
|
|
|
|
3. it is a specific version of a locale with no general locale
|
|
|
|
*/
|
2021-01-30 00:17:35 +08:00
|
|
|
return allLocales.filter(
|
2021-10-30 01:10:20 +08:00
|
|
|
(locale) => (!locale.locale.includes('-') || locale.locale.split('-')[0] === splitValue || !allLocaleCodes.includes(locale.locale.split('-')[0])),
|
2021-01-30 00:17:35 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const {
|
2023-01-09 20:42:02 +08:00
|
|
|
value, handleChange, elementId, selectMessage, ariaLabel, intl,
|
2021-01-30 00:17:35 +08:00
|
|
|
} = this.props;
|
|
|
|
const defaultLocale = value || DEFAULT_VALUE;
|
|
|
|
|
|
|
|
const availableLocales = this.filterLocaleVariations(value);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<select
|
|
|
|
id={elementId}
|
|
|
|
onChange={handleChange}
|
|
|
|
value={defaultLocale}
|
2022-12-17 00:02:46 +08:00
|
|
|
aria-label={ariaLabel||''}
|
2021-01-30 00:17:35 +08:00
|
|
|
>
|
|
|
|
<option disabled key={DEFAULT_KEY} value={DEFAULT_VALUE}>
|
|
|
|
{selectMessage}
|
|
|
|
</option>
|
2022-12-14 21:21:44 +08:00
|
|
|
{availableLocales.map((localeItem) => {
|
|
|
|
const localizedName = localeItem.locale !== value && intl.formatMessage({
|
|
|
|
id: `app.submenu.application.localeDropdown.${localeItem.locale}`,
|
|
|
|
defaultMessage: ``,
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
2023-07-31 22:24:25 +08:00
|
|
|
<option key={localeItem.locale} value={localeItem.locale} lang={localeItem.locale}>
|
2022-12-14 21:21:44 +08:00
|
|
|
{localeItem.name}{localizedName && ` - ${localizedName}`}
|
|
|
|
</option>
|
|
|
|
);
|
|
|
|
})}
|
2021-01-30 00:17:35 +08:00
|
|
|
</select>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LocalesDropdown.propTypes = propTypes;
|
|
|
|
LocalesDropdown.defaultProps = defaultProps;
|
|
|
|
|
2022-12-14 21:21:44 +08:00
|
|
|
export default injectIntl(LocalesDropdown);
|