Skip to content

Commit

Permalink
fix: Detect Language before Speak
Browse files Browse the repository at this point in the history
  • Loading branch information
Pylogmon committed Dec 22, 2023
1 parent 43803bb commit 4b0d692
Showing 1 changed file with 24 additions and 18 deletions.
42 changes: 24 additions & 18 deletions src/window/Translate/components/SourceArea/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -161,26 +161,31 @@ export default function SourceArea(props) {

const handleSpeak = async () => {
const serviceName = ttsServiceList[0];
let detected = detectLanguage;
if (detected === '') {
detected = await detect(sourceText);
setDetectLanguage(detected);
}
if (serviceName.startsWith('[plugin]')) {
if (!(detectLanguage in ttsPluginInfo.language)) {
if (!(detected in ttsPluginInfo.language)) {
throw new Error('Language not supported');
}
const config = (await store.get(serviceName)) ?? {};
const data = await invoke('invoke_plugin', {
name: serviceName,
pluginType: 'tts',
source: sourceText,
lang: ttsPluginInfo.language[detectLanguage],
lang: ttsPluginInfo.language[detected],
needs: config,
});
speak(data);
} else {
if (!(detectLanguage in builtinTtsServices[serviceName].Language)) {
if (!(detected in builtinTtsServices[serviceName].Language)) {
throw new Error('Language not supported');
}
let data = await builtinTtsServices[serviceName].tts(
sourceText,
builtinTtsServices[serviceName].Language[detectLanguage]
builtinTtsServices[serviceName].Language[detected]
);
speak(data);
}
Expand Down Expand Up @@ -334,20 +339,21 @@ export default function SourceArea(props) {
</Chip>
)}
</div>
<Button
size='sm'
color='primary'
variant='solid'
className='text-[14px] font-bold'
startContent={<HiTranslate className='text-[16px]' />}
onPress={() => {
detect_language(sourceText).then(() => {
syncSourceText();
});
}}
>
{t('translate.translate')}
</Button>
<Tooltip content={t('translate.translate')}>
<Button
size='sm'
color='primary'
variant='light'
isIconOnly
className='text-[14px] font-bold'
startContent={<HiTranslate className='text-[16px]' />}
onPress={() => {
detect_language(sourceText).then(() => {
syncSourceText();
});
}}
/>
</Tooltip>
</CardFooter>
</Card>
);
Expand Down

0 comments on commit 4b0d692

Please sign in to comment.