-
Notifications
You must be signed in to change notification settings - Fork 0
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
ce79f63
commit af9549b
Showing
11 changed files
with
88 additions
and
75 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
const getErrorInTranslationMessage = ((error: unknown) => new Error(`Error in translation: ${String(error)}`)); | ||
const getErrorInTranslationMessage = ((error: unknown) => new Error(`react-g-translator: Error in translation: ${String(error)}`)); | ||
|
||
export default getErrorInTranslationMessage; |
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 |
---|---|---|
@@ -1,24 +1,18 @@ | ||
import { translate } from '@vitalets/google-translate-api'; | ||
import enableCors from './enableCorsAndLimitRate'; | ||
import getErrorInTranslationMessage from './getErrorInTranslationMessage'; | ||
import language from '../types/language'; | ||
|
||
const getTranslation = async ( | ||
text: string | string[], | ||
from?: language, | ||
to?: language, | ||
) : Promise<string | undefined> => { | ||
try { | ||
// rate limit (1 request per second) and CORS policy overriding (if any) | ||
enableCors(1); | ||
// rate limit (1 request per second) and CORS policy overriding (if any) | ||
enableCors(1); | ||
|
||
// translating happens here. ✨ bing! ✨ | ||
const translation = await translate(text as string, { from, to }); | ||
return translation.text; | ||
} catch (error) { | ||
console.error(getErrorInTranslationMessage(error)); | ||
return undefined; | ||
} | ||
// translating happens here. ✨ bing! ✨ | ||
const translation = await translate(text as string, { from, to }); | ||
return translation.text; | ||
}; | ||
|
||
export default getTranslation; |