Skip to content

Commit

Permalink
Hide Settings in Web
Browse files Browse the repository at this point in the history
  • Loading branch information
ENDlezZenith committed Sep 21, 2024
1 parent 9983a92 commit 930332d
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 32 deletions.
50 changes: 50 additions & 0 deletions src/main/features/core/lyrics/translate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import axios from 'axios';

export const translateLyrics = async (
originalLyrics: string,
apiKey: string,
apiProvider: string | null,
targetLanguage: string | null,
) => {
let TranslatedText = '';
if (apiProvider === 'Microsoft Azure') {
try {
const response = await axios({
data: [
{
Text: originalLyrics,
},
],
headers: {
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key': apiKey,
},
method: 'post',
url: `https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&to=${targetLanguage as string}`,
});
TranslatedText = response.data[0].translations[0].text;
} catch (e) {
console.error('Microsoft Azure translate request got an error!', e);
return null;
}
} else if (apiProvider === 'Google Cloud') {
try {
const response = await axios({
data: {
format: 'text',
q: originalLyrics,
},
headers: {
'Content-Type': 'application/json',
},
method: 'post',
url: `https://translation.googleapis.com/language/translate/v2?target=${targetLanguage as string}&key=${apiKey}`,
});
TranslatedText = response.data.data.translations[0].translatedText;
} catch (e) {
console.error('Google Cloud translate request got an error!', e);
return null;
}
}
return TranslatedText;
};
40 changes: 8 additions & 32 deletions src/renderer/features/lyrics/lyrics.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { AnimatePresence, motion } from 'framer-motion';
import { ErrorBoundary } from 'react-error-boundary';
import { RiInformationFill } from 'react-icons/ri';
import styled from 'styled-components';
import axios from 'axios';
import { useSongLyricsByRemoteId, useSongLyricsBySong } from './queries/lyric-query';
import { SynchronizedLyrics, SynchronizedLyricsProps } from './synchronized-lyrics';
import { Spinner, TextTitle } from '/@/renderer/components';
Expand All @@ -18,6 +17,7 @@ import { FullLyricsMetadata, LyricSource, LyricsOverride } from '/@/renderer/api
import { LyricsActions } from '/@/renderer/features/lyrics/lyrics-actions';
import { queryKeys } from '/@/renderer/api/query-keys';
import { queryClient } from '/@/renderer/lib/react-query';
import { translateLyrics } from '/@/main/features/core/lyrics/translate';

const ActionsContainer = styled.div`
position: absolute;
Expand Down Expand Up @@ -88,7 +88,7 @@ export const Lyrics = () => {
const lyricsSettings = useLyricsSettings();
const [index, setIndex] = useState(0);
const [translatedLyrics, setTranslatedLyrics] = useState<string | null>(null);
const [showTranslation, setShowTranslation] = useState<boolean>(false);
const [showTranslation, setShowTranslation] = useState(false);

const { data, isInitialLoading } = useSongLyricsBySong(
{
Expand Down Expand Up @@ -150,36 +150,12 @@ export const Lyrics = () => {
? lyrics.lyrics.map(([, line]) => line).join('\n')
: lyrics.lyrics;
const { apiKey, apiProvider, targetLanguage } = lyricsSettings;
let TranslatedText = '';
if (apiProvider === 'Microsoft Azure') {
const response = await axios({
data: [
{
Text: originalLyrics,
},
],
headers: {
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key': apiKey,
},
method: 'post',
url: `https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&to=${targetLanguage as string}`,
});
TranslatedText = response.data[0].translations[0].text;
} else if (apiProvider === 'Google Cloud') {
const response = await axios({
data: {
format: 'text',
q: originalLyrics,
},
headers: {
'Content-Type': 'application/json',
},
method: 'post',
url: `https://translation.googleapis.com/language/translate/v2?target=${targetLanguage as string}&key=${apiKey}`,
});
TranslatedText = response.data.data.translations[0].translatedText;
}
const TranslatedText: string | null = await translateLyrics(
originalLyrics,
apiKey,
apiProvider,
targetLanguage,
);
setTranslatedLyrics(TranslatedText);
setShowTranslation(true);
}, [lyrics, lyricsSettings, translatedLyrics, showTranslation]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export const LyricSettings = () => {
/>
),
description: t('Target language for translation'),
isHidden: !isElectron(),
title: t('setting.targetLanguage', { postProcess: 'sentenceCase' }),
},
{
Expand All @@ -148,6 +149,7 @@ export const LyricSettings = () => {
/>
),
description: t('Api provider for translation'),
isHidden: !isElectron(),
title: t('setting.apiProvider', { postProcess: 'sentenceCase' }),
},
{
Expand All @@ -160,6 +162,7 @@ export const LyricSettings = () => {
/>
),
description: t('Api key for translation'),
isHidden: !isElectron(),
title: t('setting.apiKey', { postProcess: 'sentenceCase' }),
},
];
Expand Down

0 comments on commit 930332d

Please sign in to comment.