-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranslator.py
More file actions
25 lines (22 loc) · 723 Bytes
/
translator.py
File metadata and controls
25 lines (22 loc) · 723 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from googletrans import Translator
def translate_text(text, target_language):
"""
translate a text to a given language
params:
text: the text to translate
target_language: the language to translate to
values : ['en', 'ar', 'fr', 'es', 'it', 'de', 'pt', 'ru', 'ja', 'ko', 'zh-cn', 'tr']
return: the translated text
"""
# Creation d'une instance de la classe Translator
translator = Translator()
# traduction du texte
translation = translator.translate(text, dest=target_language)
return translation.text
# example
"""
text = "Hello, how are you?"
target_language = 'es'
translated_text = translate_text(text, target_language)
print(translated_text)
"""