Reputation: 19
so I got a basic CLI python translator to function, now I need help pulling just the translated text from the result.
This is my current code
from googletrans import Translator
# Taking input for language to be translated
translate_from = input("What language would you like to translate from? ")
translate_to = input("What language would you like to translate to? ")
# Taking input for text to be translated
translate_text = input("Please enter your text to be translated ... ")
# translate languages
translator=Translator()
print("Translation > ", translator.translate(translate_text, src=translate_from, dest=translate_to))
This is the current output:
Translation > Translated(src=en, dest=es, text=Hola, pronunciation=None, extra_data="{'translat...")
I would like it to just pull the text only instead of the above. Can anyone help me on this because I am pretty stumped!
Also I am very new to Python, so please go easy on me!
Upvotes: 1
Views: 1480
Reputation: 1
Text-to-Text: Translating text from one language to another
Install googletrans pip install googletrans==4.0.0-rc1
Fixed Code:
from googletrans import Translator
# Input for source and target languages
translate_from = input("Translate from (e.g., 'en'): ")
translate_to = input("Translate to (e.g., 'bn'): ")
# Input for text
translate_text = input("Enter text to translate: ")
# Translate the text
translator = Translator()
translation =
translator.translate(translate_text,src=translate_from,
dest=translate_to)
# Print the result
print("Translation:", translation.text)
Input
Translate from (e.g., 'en'): en
Translate to (e.g., 'bn'): bn
Enter text to translate: Hello, how are you?
Output
Translation: হ্যালো, আপনি কেমন আছেন?
Upvotes: 0
Reputation: 15738
translator.translate
returns a Translated
instance. It stores translated text in its .text
property. So,
translation = translator.translate(...)
print("Translation > ", translation.text)
Or, when a list of strings was passed for translation,
translations = translator.translate([string1, string2, ...], ...)
for translation in translations:
print("Translation > ", translation.text)
Upvotes: 1