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)
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)
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)