Skip to content

Instantly share code, notes, and snippets.

@Davisy
Created January 9, 2021 07:36
Show Gist options
  • Save Davisy/ea658746f6f5f55518301349f066085e to your computer and use it in GitHub Desktop.
Save Davisy/ea658746f6f5f55518301349f066085e to your computer and use it in GitHub Desktop.
from langdetect import detect
from google_trans_new import google_translator
#simple function to detect and translate text
def detect_and_translate(text,target_lang):
result_lang = detect(text)
if result_lang == target_lang:
return text
else:
translator = google_translator()
translate_text = translator.translate(text,lang_src=result_lang,lang_tgt=target_lang)
return translate_text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment