def translate(self): client_id = "solwit_intern_project" client_secret = "Ffon8CQ2sbCdXgnm2oc6SrR+ZgE8lZVHgFNXA5lLi0Q=" translator = Translator(client_id, client_secret) characters = 0 self.draft_translated_output = self.draft_output master = self.draft_translated_output reference = self.imported_master for i in range(0,3): print (i) entry = master[i][0] print ("entry len", len(entry)) for key in entry: #print ("key", key) #print ("entry[key", entry[key]) if entry[key] == "": if key in reference: en_word = reference[key] #print ("en word", en_word, len(en_word)) characters = characters + len(en_word) entry[key] = translator.translate(en_word, "es") else: pass #print ("Not in eng", key) print ("PER TRANSLATION:", characters) return (master)
def __init__(self, room, bot, client): if bot.no_input: client_id = os.getenv("BING_ID") secret = os.getenv("BING_SECRET") if None in (client_id, secret): return else: print( "If you would like to have the translate command, please give your Bing translation credentials. If you aren't willing, just hit Enter to skip this feature." ) client_id = input("Client ID: ") if not client_id: return secret = getpass("Secret: ") if not secret: return self.translator = Translator(client_id, secret) self.parser = _parser.Parser(['from', 'to'], self.parse_token) bot.register( "translate", self.on_translate, help= "Translate word/phrase using Bing's translation API. By default, the source language is guessed, and the target language is English, but you can specify with the `from` and `to` keywords (at the end of the command). Multi-word texts should be in quotation marks." )
async def cmd_translate(message, arg): tolang, text = arg.split(' ', 1) if len( text ) > 100: #maybe it's wise to put a limit on the lenght of the translations await client.send_message( message.channel, "Your text is too long: Max allowed is 100 characters.") return translator = Translator(client_id, client_secret) translation = translator.translate(text, tolang) await client.send_message(message.channel, translation)
class TestAll(unittest.TestCase): def setUp(self): self.translator = Translator(CLIENT_ID, CLIENT_SECRET) def test_detect_language(self): lang_detect = self.translator.detect("Hello World") self.assertEqual("en", lang_detect) def test_detect_languages(self): langs_detected = self.translator.detect_texts(["Hello World", "Voe sem parar."]) self.assertEqual(["en", "pt"], langs_detected) def test_translate_method(self): trans = self.translator.translate("Oi", "en") self.assertEqual("Hi", trans)
class TestAll(unittest.TestCase): def setUp(self): self.translator = Translator(CLIENT_ID, CLIENT_SECRET) def test_detect_language(self): lang_detect = self.translator.detect("Hello World") self.assertEqual("en", lang_detect) def test_detect_languages(self): langs_detected = self.translator.detect_texts( ["Hello World", "Voe sem parar."]) self.assertEqual(["en", "pt"], langs_detected) def test_translate_method(self): trans = self.translator.translate("Oi", "en") self.assertEqual("Hi", trans)
async def cmd_translate(client, message, arg): usage = ( "Usage: `!translate [<from> <to>] <text>`\n" "If `to` and `from` are not set, automatic detection is attempted and the text translated to english.\n" "Maximum of 100 characters is allowed.\n") def valid(arg): return 0 < len(arg) < 100 arg = arg.strip() if not valid(arg): await message.channel.send(usage) return fromlang, tolang, input = parse(arg) bing_client_id = os.environ['BING_CLIENTID'] bing_client_secret = os.environ['BING_SECRET'] translator = Translator(bing_client_id, bing_client_secret) translation = translator.translate(input, tolang, fromlang) await message.channel.send(translation)
def setUp(self): self.translator = Translator(CLIENT_ID, CLIENT_SECRET)
class main: def __init__(self, room, bot, client): if bot.no_input: client_id = os.getenv("BING_ID") secret = os.getenv("BING_SECRET") if None in (client_id, secret): return else: print("If you would like to have the translate command, please give your Bing translation credentials. If you aren't willing, just hit Enter to skip this feature.") client_id = eval(input("Client ID: ")) if not client_id: return secret = getpass("Secret: ") if not secret: return self.translator = Translator(client_id, secret) self.parser = _parser.Parser(['from', 'to'], self.parse_token) bot.register("translate", self.on_translate, help="Translate word/phrase using Bing's translation API. By default, the source language is guessed, and the target language is English, but you can specify with the `from` and `to` keywords (at the end of the command). Multi-word texts should be in quotation marks.") def parse_token(self, token, args): num_args = len(args) if num_args == 0: raise UnknownLanguageError("") elif num_args == 1: try: return self.normalize(args[0]) except ValueError: raise UnknownLanguageError("") else: try: return self.normalize(" ".join(args)) except ValueError: return False def on_translate(self, event, room, client, bot): words = event.args keys = {'from': 'from_lang', 'to': 'to_lang'} dic = {keys['to']: 'en'} try: parsed = self.parser.parse(words) leftover = parsed.pop('leftover') if len(leftover) > 1: event.message.reply("Either you didn't put your text in quotation marks, or you're trying to use commands that I don't understand.") else: text = " ".join(leftover) for key, value in list(parsed.items()): dic[keys[key]] = value event.message.reply(self.translator.translate(text, **dic), False) except UnknownLanguageError as e: event.message.reply("I don't understand what {!r} is.".format(e.language)) def normalize(self, lang): # Normalized good locale codes will strip the spaces, but bad ones # remain as is. normalized = locale.normalize(lang) if normalized == lang and locale.normalize(lang + ' ') == lang + ' ': lang = lang.strip() raise ValueError("Unknown locale code: {}".format(lang), lang) return normalized[:2]
from BingTranslator import Translator client_id = "BookAboutPyQt5" client_secret = "RWfmb4O7eO3zbnlTqZaPu8cBmMthaXkonxQA9sQnQ+0=" translator = Translator(client_id, client_secret) phrase_translated = translator.translate("Hello World", "pt") #translating phrase print (phrase_translated)