def _deep_translate(self, original_language): """ This function is used to translated a word from it source language, such as Spanish into American English. :param original_language: language to translated from :return: translated word :rtype: string """ try: translator = deepl.Translator(auth_key=self._api_key) result = translator.translate_text(self._str_to_translate, target_lang='EN-US', source_lang=original_language) translated_text = result.text return translated_text except QuotaExceededException as error: """ The exception is thrown when the Quota for this billing period has been exceeded. """ print(self._colorized_text(255, 0, 0, 'The quota for the the Deep translation service for this billing ' 'period has been exceeded.')) logger.error('The quota for the the Deep translation service for this billing period has been exceeded.') logger.error(''.join(traceback.format_tb(error.__traceback__))) except TooManyRequestsException as error: """ The exception is thrown if an error occurred during the request call, e.g a connection problem. """ logger.error('Server Error: There has been too many requests to the server.') logger.error(''.join(traceback.format_tb(error.__traceback__))) except TypeError as error: """ The exception is thrown when an operation or function is applied to an object of inappropriate type. """ logger.error(f'A TypeError occurred when attempting to translate the word {self._str_to_translate}') logger.error(''.join(traceback.format_tb(error.__traceback__))) except ValueError as error: """ The exception is thrown when an operation or function receives an argument that has the right type but an inappropriate value. """ if str(error) == "auth_key must not be empty": print(self._colorized_text(255, 0, 0, 'An Authorization failure has occurred when using the Deep ' 'translation service. Please verify your authentication key')) logger.error('An Authorization key cannot be empty when using the Deep translation service.') logger.error(''.join(traceback.format_tb(error.__traceback__))) elif str(error) == 'text must not be empty': print(self._colorized_text(255, 0, 0, 'An empty string was passed to the Deep translation service.')) logger.error('An empty string was passed to the Deep translation service.') logger.error(''.join(traceback.format_tb(error.__traceback__))) else: logger.error(f'An unknown ValueError occurred when attempting to translate the word' f' {self._str_to_translate}') logger.error(''.join(traceback.format_tb(error.__traceback__)))
def __init__(self, lang, api_key, omegat_project=None, glossary=None): self.targetlang = lang self.translator = deepl.Translator(api_key) # load tmx with existing translations self.translated = {} if omegat_project: self.translated = self.load_tmx( f"{omegat_project}/omegat/project_save.tmx") # load tmx with existing translations self.glossary = {} if glossary: self.glossary = self.loadCSV(glossary)
def __init__(self, bot: commands.Bot): super(Translate, self).__init__(bot, __file__) self.translator = deepl.Translator( auth_key=Config.translate_deepl_api_key) self.supp_list = [ t.code for t in self.translator.get_target_languages() ] self.flag_to_code = { (flag.flag(c) if not "-" in c else flag.flag(c.split("-")[1])): c for c in self.supp_list } self.flag_to_code = {**self.flag_to_code, **special_flags} self.guilds_usage = {} if not self.clear_usage_task.is_running(): self.clear_usage_task.start()
import asyncio import deepl text = 'I have a pen.' translator = deepl.Translator(deepl.AiohttpAdapter('Your API key')) async def main(): print(await translator.translate(text, deepl.TargetLang.Japanese)) loop = asyncio.get_event_loop() loop.run_until_complete(main())
def __init__(self): self.translator = deepl.Translator(DEEPL_AUTH_KEY)
def get_provider(self): return deepl.Translator(auth_key=self.__api_key)
import deepl text = 'I have a pen.' translator = deepl.Translator(deepl.RequestsAdapter('Your API key')) def main(): print(translator.translate(text, deepl.TargetLang.Japanese)) if __name__ == '__main__': main()
#for f in MTL_input/*.txt; do ./machineTranslator.py "$f" "./MTL_output/$(basename $f)"; done #If a sentence only contains these characters, filter it out bannedSentences = set('…。!?,.…-—!') def checkIsBanned(test_str): return set(test_str) <= bannedSentences replacementDict = {"?": "?", "!": "!", ":": ":", ";": ";"} print(sys.argv[1]) translator = deepl.Translator(os.environ['DEEPL_API_KEY']) usage = translator.get_usage() print("Used " + str(usage.character.count) + " of " + str(usage.character.limit) + ".") with open(sys.argv[1], 'r') as inputFile: with open(sys.argv[2], 'w+') as outputFile: lines = inputFile.readlines() for i in range(len(lines)): line = unicodedata.normalize("NFKC", lines[i]) if not line.strip(): continue elif ":" not in line: outputFile.write(line) continue cmds, text = line.split(':', 1)