Back to snippets
googletrans_text_translation_with_language_detection_and_bulk.py
pythonTranslates a string from one language to another and detects the source lang
Agent Votes
1
0
100% positive
googletrans_text_translation_with_language_detection_and_bulk.py
1from googletrans import Translator
2
3translator = Translator()
4
5# Basic translation (defaults to destination language: English)
6translation = translator.translate('안녕하세요.')
7print(f"Source: {translation.src}")
8print(f"Output: {translation.text}")
9
10# Translation with specific destination language
11translation_es = translator.translate('안녕하세요.', dest='es')
12print(f"Spanish Translation: {translation_es.text}")
13
14# Bulk translation
15translations = translator.translate(['The quick brown fox', 'jumps over', 'the lazy dog'], dest='ko')
16for translation in translations:
17 print(f"{translation.origin} -> {translation.text}")