1
0
mirror of https://github.com/tooot-app/app synced 2025-01-01 04:08:08 +01:00

Improve translation detection

This commit is contained in:
Zhiyuan Zheng 2022-06-10 16:52:55 +02:00
parent 0864f7373a
commit f238413232

View File

@ -26,9 +26,6 @@ const TimelineTranslate = React.memo(
if (!highlighted) { if (!highlighted) {
return null return null
} }
if (!status.language) {
return null
}
const { t } = useTranslation('componentTimeline') const { t } = useTranslation('componentTimeline')
const { colors } = useTheme() const { colors } = useTheme()
@ -45,10 +42,6 @@ const TimelineTranslate = React.memo(
const [detectedLanguage, setDetectedLanguage] = useState<string>('') const [detectedLanguage, setDetectedLanguage] = useState<string>('')
useEffect(() => { useEffect(() => {
if (!status.language) {
return
}
const detect = async () => { const detect = async () => {
const result = await detectLanguage(text.join(`\n`)) const result = await detectLanguage(text.join(`\n`))
setDetectedLanguage(result.detected.slice(0, 2)) setDetectedLanguage(result.detected.slice(0, 2))
@ -65,14 +58,14 @@ const TimelineTranslate = React.memo(
text, text,
options: { enabled } options: { enabled }
}) })
console.log('detectedLanguage', detectedLanguage)
if (!detectedLanguage) { if (!detectedLanguage) {
return null return null
} }
if (Localization.locale.includes(detectedLanguage)) { if (Localization.locale.slice(0, 2).includes(detectedLanguage)) {
return null return null
} }
if (settingsLanguage?.includes(detectedLanguage)) { if (settingsLanguage?.slice(0, 2).includes(detectedLanguage)) {
return null return null
} }
@ -89,13 +82,13 @@ const TimelineTranslate = React.memo(
if (enabled) { if (enabled) {
if (!isSuccess) { if (!isSuccess) {
analytics('timeline_shared_translate_retry', { analytics('timeline_shared_translate_retry', {
language: status.language language: detectedLanguage
}) })
refetch() refetch()
} }
} else { } else {
analytics('timeline_shared_translate', { analytics('timeline_shared_translate', {
language: status.language language: detectedLanguage
}) })
setEnabled(true) setEnabled(true)
} }
@ -125,7 +118,7 @@ const TimelineTranslate = React.memo(
</CustomText> </CustomText>
<CustomText> <CustomText>
{__DEV__ {__DEV__
? ` Source: ${status.language}; Target: ${ ? ` Source: ${detectedLanguage}; Target: ${
Localization.locale || settingsLanguage || 'en' Localization.locale || settingsLanguage || 'en'
}` }`
: undefined} : undefined}
@ -153,7 +146,6 @@ const TimelineTranslate = React.memo(
) )
}, },
(prev, next) => (prev, next) =>
prev.status.language === next.status.language &&
prev.status.content === next.status.content && prev.status.content === next.status.content &&
prev.status.spoiler_text === next.status.spoiler_text prev.status.spoiler_text === next.status.spoiler_text
) )