2019-09-04 23:43:37 +02:00
|
|
|
// @flow
|
|
|
|
|
|
|
|
import React, { useState } from 'react';
|
|
|
|
import { FormField } from 'component/common/form';
|
|
|
|
import Spinner from 'component/spinner';
|
|
|
|
import { SETTINGS } from 'lbry-redux';
|
2019-10-15 17:37:55 +02:00
|
|
|
import SUPPORTED_LANGUAGES from '../../constants/supported_languages';
|
2019-09-04 23:43:37 +02:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
language: string,
|
|
|
|
showToast: ({}) => void,
|
|
|
|
setClientSetting: (string, boolean) => void,
|
|
|
|
};
|
|
|
|
|
|
|
|
function SettingLanguage(props: Props) {
|
|
|
|
const [isFetching, setIsFetching] = useState(false);
|
2019-10-15 17:37:55 +02:00
|
|
|
const languages = SUPPORTED_LANGUAGES;
|
2019-10-03 18:55:21 +02:00
|
|
|
|
|
|
|
const { language, showToast, setClientSetting } = props;
|
2019-09-04 23:43:37 +02:00
|
|
|
|
|
|
|
function onLanguageChange(e) {
|
|
|
|
const { value } = e.target;
|
|
|
|
setIsFetching(true);
|
|
|
|
|
|
|
|
// this should match the behavior/logic in the static index-XXX.html files
|
|
|
|
fetch('https://lbry.com/i18n/get/lbry-desktop/app-strings/' + value + '.json')
|
|
|
|
.then(r => r.json())
|
|
|
|
.then(j => {
|
|
|
|
window.i18n_messages[value] = j;
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
setIsFetching(false);
|
|
|
|
window.localStorage.setItem(SETTINGS.LANGUAGE, value);
|
|
|
|
setClientSetting(SETTINGS.LANGUAGE, value);
|
|
|
|
})
|
|
|
|
.catch(e => {
|
|
|
|
showToast({
|
|
|
|
message: __('Failed to load translations.'),
|
|
|
|
error: true,
|
|
|
|
});
|
|
|
|
setIsFetching(false);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<React.Fragment>
|
|
|
|
<FormField
|
|
|
|
name="language_select"
|
|
|
|
type="select"
|
|
|
|
label={__('Language')}
|
|
|
|
onChange={onLanguageChange}
|
|
|
|
value={language}
|
|
|
|
helper={__(
|
|
|
|
'Multi-language support is brand new and incomplete. Switching your language may have unintended consequences, like glossolalia.'
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
{Object.keys(languages).map(language => (
|
|
|
|
<option key={language} value={language}>
|
|
|
|
{languages[language]}
|
|
|
|
</option>
|
|
|
|
))}
|
|
|
|
</FormField>
|
|
|
|
{isFetching && <Spinner type="small" />}
|
|
|
|
</React.Fragment>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default SettingLanguage;
|