2022-12-10 14:16:06 -08:00
|
|
|
import { List as ImmutableList } from 'immutable';
|
2022-10-27 10:46:03 -07:00
|
|
|
import React from 'react';
|
|
|
|
import { FormattedMessage, useIntl } from 'react-intl';
|
|
|
|
|
|
|
|
import { translateStatus, undoStatusTranslation } from 'soapbox/actions/statuses';
|
2022-12-10 14:16:06 -08:00
|
|
|
import { useAppDispatch, useAppSelector, useFeatures, useInstance } from 'soapbox/hooks';
|
2023-01-08 14:04:19 -08:00
|
|
|
import { isLocal } from 'soapbox/utils/accounts';
|
2022-10-27 10:46:03 -07:00
|
|
|
|
2023-01-30 14:53:44 -08:00
|
|
|
import { Stack, Button, Text } from './ui';
|
2022-10-27 10:46:03 -07:00
|
|
|
|
2023-01-08 14:04:19 -08:00
|
|
|
import type { Account, Status } from 'soapbox/types/entities';
|
2022-10-27 10:46:03 -07:00
|
|
|
|
|
|
|
interface ITranslateButton {
|
2023-02-15 13:26:27 -08:00
|
|
|
status: Status
|
2022-10-27 10:46:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const TranslateButton: React.FC<ITranslateButton> = ({ status }) => {
|
|
|
|
const dispatch = useAppDispatch();
|
|
|
|
const intl = useIntl();
|
|
|
|
const features = useFeatures();
|
2022-12-10 14:16:06 -08:00
|
|
|
const instance = useInstance();
|
2022-10-27 10:46:03 -07:00
|
|
|
|
|
|
|
const me = useAppSelector((state) => state.me);
|
|
|
|
|
2023-01-08 14:04:19 -08:00
|
|
|
const allowUnauthenticated = instance.pleroma.getIn(['metadata', 'translation', 'allow_unauthenticated'], false);
|
|
|
|
const allowRemote = instance.pleroma.getIn(['metadata', 'translation', 'allow_remote'], true);
|
|
|
|
|
2022-12-10 14:16:06 -08:00
|
|
|
const sourceLanguages = instance.pleroma.getIn(['metadata', 'translation', 'source_languages']) as ImmutableList<string>;
|
|
|
|
const targetLanguages = instance.pleroma.getIn(['metadata', 'translation', 'target_languages']) as ImmutableList<string>;
|
|
|
|
|
2023-01-08 14:04:19 -08:00
|
|
|
const renderTranslate = (me || allowUnauthenticated) && (allowRemote || isLocal(status.account as Account)) && ['public', 'unlisted'].includes(status.visibility) && status.contentHtml.length > 0 && status.language !== null && intl.locale !== status.language;
|
2022-10-27 10:46:03 -07:00
|
|
|
|
2022-12-10 14:16:06 -08:00
|
|
|
const supportsLanguages = (!sourceLanguages || sourceLanguages.includes(status.language!)) && (!targetLanguages || targetLanguages.includes(intl.locale));
|
|
|
|
|
2022-10-27 10:46:03 -07:00
|
|
|
const handleTranslate: React.MouseEventHandler<HTMLButtonElement> = (e) => {
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
|
|
if (status.translation) {
|
|
|
|
dispatch(undoStatusTranslation(status.id));
|
|
|
|
} else {
|
2022-11-04 14:36:39 -07:00
|
|
|
dispatch(translateStatus(status.id, intl.locale));
|
2022-10-27 10:46:03 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-12-10 14:16:06 -08:00
|
|
|
if (!features.translations || !renderTranslate || !supportsLanguages) return null;
|
2022-10-27 10:46:03 -07:00
|
|
|
|
|
|
|
if (status.translation) {
|
|
|
|
const languageNames = new Intl.DisplayNames([intl.locale], { type: 'language' });
|
|
|
|
const languageName = languageNames.of(status.language!);
|
|
|
|
const provider = status.translation.get('provider');
|
|
|
|
|
|
|
|
return (
|
2023-01-30 14:53:44 -08:00
|
|
|
<Stack space={3} alignItems='start'>
|
|
|
|
<Button
|
|
|
|
theme='muted'
|
|
|
|
text={<FormattedMessage id='status.show_original' defaultMessage='Show original' />}
|
|
|
|
icon={require('@tabler/icons/language.svg')}
|
|
|
|
onClick={handleTranslate}
|
|
|
|
/>
|
|
|
|
<Text theme='muted'>
|
2023-01-26 15:23:55 -08:00
|
|
|
<FormattedMessage id='status.translated_from_with' defaultMessage='Translated from {lang} using {provider}' values={{ lang: languageName, provider }} />
|
2023-01-30 14:53:44 -08:00
|
|
|
</Text>
|
2022-10-27 10:46:03 -07:00
|
|
|
</Stack>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2023-01-30 14:53:44 -08:00
|
|
|
<div>
|
|
|
|
<Button
|
|
|
|
theme='muted'
|
|
|
|
text={<FormattedMessage id='status.translate' defaultMessage='Translate' />}
|
|
|
|
icon={require('@tabler/icons/language.svg')}
|
|
|
|
onClick={handleTranslate}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
2022-10-27 10:46:03 -07:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default TranslateButton;
|