File size: 806 Bytes
83d5183
 
037fa35
26f365f
 
83d5183
 
 
26f365f
 
 
dcd3020
83d5183
26f365f
83d5183
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from deep_translator import GoogleTranslator, detection
import os

available_languages = GoogleTranslator().get_supported_languages(as_dict=True)
formatted_languages = {key.title(): value for key, value in available_languages.items()}
formatted_codes = {value: key.title() for key, value in available_languages.items()}

lang_detect_key = os.getenv("detect_language_api_key")

def src_txt_to_eng_translator(input_text, target_lang = 'English'):
    target_lang_code = formatted_languages.get(target_lang, 'en')
    src_lang_code = detection.single_detection(input_text, api_key = lang_detect_key)
    src_lang = formatted_codes.get(src_lang_code, 'Source language not detected')
    translated = GoogleTranslator(source='auto', target=target_lang_code).translate(input_text)
    return translated, src_lang