示例#1
0
def gtrans(update, context):
    msg = update.effective_message
    args = context.args
    lang = " ".join(args)
    if not lang:
        lang = "en"
    try:
        translate_text = (msg.reply_to_message.text
                          or msg.reply_to_message.caption
                          or msg.reply_to_message.poll.question)
    except AttributeError:
        return msg.reply_text("Give me the text to translate!")

    ignore_text = UNICODE_EMOJI.keys()
    for emoji in ignore_text:
        if emoji in translate_text:
            translate_text = translate_text.replace(emoji, "")

    translator = google_translator()
    try:
        translated = translator.translate(translate_text, lang_tgt=lang)
        source_lan = translator.detect(translate_text)[1].title()
        des_lan = LANGUAGES.get(lang).title()
        msg.reply_text("Translated from {} to {}.\n {}".format(
            source_lan, des_lan, translated))
    except BaseException:
        msg.reply_text("Error! invalid language code.")
示例#2
0
async def gtrans(client, message):
    args = message.text.split(None, 1)
    if len(args) == 2:
        lang = args[1]
    else:
        lang = "en"
    try:
        translate_text = message.reply_to_message.text or message.reply_to_message.caption
    except AttributeError:
        await message.reply_text("What text I should translate?")
        return
    ignore_text = UNICODE_EMOJI.keys()
    for emoji in ignore_text:
        if emoji in translate_text:
            translate_text = translate_text.replace(emoji, "")

    translator = google_translator()
    try:
        translated = translator.translate(translate_text, lang_tgt=lang)
        source_lan = translator.detect(translate_text)[1].title()
        des_lan = LANGUAGES.get(lang).title()
        await message.reply_text("Translated from {} to {}.\n {}".format(
            source_lan, des_lan, translated))
    except BaseException as excp:
        await message.reply_text("Error: {}".format(excp))
示例#3
0
async def translateme(trans):
    """ For .trt command, translate the given text using Google Translate. """
    translator = Translator()
    g = async_google_trans_new.AsyncTranslator()
    detector = tr()
    textx = await trans.get_reply_message()
    message = trans.pattern_match.group(1)
    if message:
        pass
    elif textx:
        message = textx.text
    else:
        await trans.edit("`Give a text or reply to a message to translate!`")
        return
    try:
        reply_text = await g.translate(deEmojify(message), TRT_LANG)
    except ValueError:
        await trans.edit("Invalid destination language.")
        return

    try:
        source_lan = await detector.detect(deEmojify(message))
        source_lan = LANGUAGES.get(source_lan).title()

    except:
        source_lan = "(Google didn't provide this info.)"

    reply_text = f"From: **{source_lan}**\nTo: **{LANGUAGES.get(TRT_LANG).title()}**\n\n{reply_text}"

    await trans.edit(reply_text)
    if BOTLOG:
        await trans.client.send_message(
            BOTLOG_CHATID,
            f"Translated some {source_lan.title()} stuff to {LANGUAGES[TRT_LANG].title()} just now.",
        )
示例#4
0
def gtrans(update, context):
    msg = update.effective_message
    args = context.args
    lang = " ".join(args)
    if not lang:
        lang = "en"
    try:
        translate_text = (
            msg.reply_to_message.text or msg.reply_to_message.caption
        )
    except AttributeError:
        return msg.reply_text("Beri aku teks untuk diterjemahkan!")

    ignore_text = UNICODE_EMOJI.keys()
    for emoji in ignore_text:
        if emoji in translate_text:
            translate_text = translate_text.replace(emoji, "")

    translator = google_translator()
    try:
        translated = translator.translate(translate_text, lang_tgt=lang)
        source_lan = translator.detect(translate_text)[1].title()
        des_lan = LANGUAGES.get(lang).title()
        msg.reply_text(
            "Diterjemahkan dari {} ke {}.\n {}".format(
                source_lan, des_lan, translated
            )
        )
    except BaseException:
        msg.reply_text("Kesalahan! kode bahasa tidak valid.")
from google_trans_new import google_translator, LANGUAGES

print("*********************************************")
print("*          THE LANGUAGE TRANSLATOR          *")
print("*********************************************\n\n")
key_list = list(LANGUAGES.keys())
val_list = list(LANGUAGES.values())
choice = 1
while (choice != 3):
    print(
        "1. All the languages available\n2. Translate a Sentence\n3. Exit\n\n")
    choice = int(input("Enter your choice\n"))

    if (choice == 1):
        for i in val_list:
            print(i, end=" , ")
        print("\n")
        continue
    elif (choice == 2):
        sor = input("Enter the source language\n")
        des = input("Enter the target language\n")
        sen = input("Enter the Sentence\n")
        sor = sor.lower()
        des = des.lower()
        sor = val_list.index(sor)
        des = val_list.index(des)
        sk = key_list[sor]
        dk = key_list[des]
        translator = google_translator()
        translate_text = translator.translate(sen, lang_src=sk, lang_tgt=dk)
        print("The Translated text is:")
示例#6
0
screenshot_path = dir_name + '\\screenshot.png'


def On_press_func(key):
    try:
        if key.char == 't':
            screenshot(screenshot_path)
            image = Image.open(screenshot_path)
            x_pos, y_pos = position().x, position().y
            cropped = image.crop(
                (x_pos - tfw, y_pos - tfh, x_pos + tfw, y_pos + tfh))
            cropped.save(screenshot_path)
            text = image_to_string(Image.open(screenshot_path))
            for word in text.split():
                src_language = translator.detect(word)[0]
                result = translator.translate(word,
                                              lang_src=src_language,
                                              lang_tgt=dest_language)
                print('\n' + word + ' -> ' + result)
    except:
        pass


if dest_language in LANGUAGES.keys() and dir_name:
    listener = keyboard.Listener(on_press=On_press_func)
    listener.start()
    listener.join()
else:
    print('\nOperation failed :(')
    input()