-
-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9983a92
commit 930332d
Showing
3 changed files
with
61 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters