-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlanguage_translation_bot.py
More file actions
69 lines (63 loc) · 2.71 KB
/
language_translation_bot.py
File metadata and controls
69 lines (63 loc) · 2.71 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# -*- coding: utf-8 -*-
import os
from googletrans import Translator, LANGUAGES
def print_banner():
banner = """
############################################################
# #
# LANGUAGE TRANSLATION BOT - PYTHON 2.7 #
# Multilingual Translation Tool #
# #
############################################################
"""
print(banner)
def list_languages():
print("Supported Languages:")
for code, lang in LANGUAGES.items():
print("{}: {}".format(code, lang))
def translate_text(text, src_lang, dest_lang):
translator = Translator()
try:
result = translator.translate(text, src=src_lang, dest=dest_lang)
return result.text
except Exception as e:
return "Error during translation: {}".format(str(e))
def main():
print_banner()
print("Welcome to the Language Translation Bot!")
while True:
print("\nOptions:")
print("1. Translate Text")
print("2. Translate Text File (Batch)")
print("3. List Supported Languages")
print("4. Exit")
choice = raw_input("Enter your choice (1-4): ").strip()
if choice == '1':
text = raw_input("Enter text to translate: ")
src_lang = raw_input("Enter source language code (or 'auto' to detect): ")
dest_lang = raw_input("Enter target language code: ")
result = translate_text(text, src_lang, dest_lang)
print("\nTranslated Text: {}".format(result))
elif choice == '2':
file_path = raw_input("Enter file path for batch translation: ")
if os.path.exists(file_path):
with open(file_path, 'r') as file:
lines = file.readlines()
src_lang = raw_input("Enter source language code (or 'auto' to detect): ")
dest_lang = raw_input("Enter target language code: ")
print("\nTranslating...")
for line in lines:
translated_line = translate_text(line.strip(), src_lang, dest_lang)
print("Original: {}".format(line.strip()))
print("Translated: {}\n".format(translated_line))
else:
print("File not found: {}".format(file_path))
elif choice == '3':
list_languages()
elif choice == '4':
print("Thank you for using the Language Translation Bot!")
break
else:
print("Invalid choice. Please try again.")
if __name__ == "__main__":
main()