This is a simple Flask application that provides a translation API using python googletrans.
Note on library usage (from googletrans)
- The maximum character limit on a single text is 15k.
- Due to limitations of the web version of google translate, this API does not guarantee that the library would work properly at all times. (so please use this library if you don’t care about stability.)
- If you want to use a stable API, I highly recommend you to use Google’s official translate API.
- If you get HTTP 5xx error or errors like #6, it’s probably because Google has banned your client IP address.
-
Environment
python==3.10
-
Clone the repository:
git clone https://github.com/chienhung1519/translation-api.git
-
Install the required dependencies:
pip install -r requirements.txt
-
Start the Flask server:
python translation_app.py --host 0.0.0.0 --port 6868
-
Send a POST request to
http://localhost:6868/translate
with the following JSON payload:{ "text": "Hello, world!", "target_language": "zh-tw" }
Replace
"Hello, world!"
with the text you want to translate, and"zh-tw"
with the target language code. -
The API will respond with the translated text:
{ "translated_text": "你好世界!" }
-
Example:
curl -X POST http://localhost:6868/translate \ -H 'Content-Type: application/json' \ -d '{"text":"Hello, world!", "target_language": "zh-tw"}' # {"translated_text": "你好世界!"}