from langdetect import detect
from google_trans_new import google_translator

def translate(text, target_lang='en'):
    try:
        input_lang = detect(text)
    except Exception as e:
        print(f'error when detecting language: {e}')
        return ''
    if input_lang == target_lang: return text
    translator = google_translator()
    try:
        text_trans = translator.translate(text, lang_src=input_lang, lang_tgt=target_lang)
    except Exception as e:
        print(f'error when translating text: {e}')
        return ''
    return text_trans
