googletrans
is a Python library for translating text, using Google Translate API. It can translate text from one language to another, and supports automatic detection of multiple languages. Here are basic usage examples:
Installgoogletrans
Library
Run the following command to install in the terminal or command line:
pip install googletrans==4.0.0-rc1
Example of usage
from googletrans import Translator # Initialize the translatortranslator = Translator() # Translation exampletext = "Hello, how are you?" translated = (text, dest='zh-cn') # Translate text to Simplified Chineseprint(f"original: {text}") print(f"translate: {}")
Main methods
-
translate(text, dest)
: Used to translate text, wheretext
It's the content to be translated.dest
is the code of the target language (e.g.'en'
Indicates English,'zh-cn'
Represents Simplified Chinese). -
detect(text)
: Automatically detect the language of the input text.
Language code examples
Common language codes are:
'en'
: English'zh-cn'
: Simplified Chinese'ja'
: Japanese'fr'
: French'de'
: German
Example: Detection language
detected = ("Hola, ¿cómo estás?") print(f"Language detection: {}") # Output: 'es' Indicates Spanish
This is the end of this article about the use of the Python Googletrans library. For more related content on Python Googletrans, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!