Here’s how to automatically translate English text into Chinese on an Ubuntu terminal using ChatGPT (OpenAI API) — with simple examples you can run right away.
✅ What You Need First
Ubuntu terminal
OpenAI API Key
If you don’t have one, create it at https://platform.openai.com/ and copy your key.Install
jq(for JSON in shell)sudo apt update sudo apt install jqSet your API key in your environment
export OPENAI_API_KEY="sk-…your key…"
๐ง Option A — Bash + curl (Simple & Fast)
You can call the Chat Completions API to translate text.
๐ Create a file called translate.sh:
#!/usr/bin/env bash
text="$1"
if [ -z "$text" ]; then
echo "Usage: ./translate.sh \"Your English text here\""
exit 1
fi
response=$(curl -s https://api.openai.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d "{
\"model\": \"gpt-4o-mini\",
\"messages\": [
{\"role\": \"system\", \"content\": \"You are a helpful translator.\"},
{\"role\": \"user\", \"content\": \"Translate to Chinese: $text\"}
],
\"max_tokens\": 200
}")
echo "$response" | jq -r '.choices[0].message.content'
Make it executable:
chmod +x translate.sh
Then run:
./translate.sh "Hello, how are you?"
๐ Option B — Python Script
If you want more flexibility, use Python:
Create
translate.py
import os
import openai
openai.api_key = os.getenv("OPENAI_API_KEY")
def translate_to_chinese(text):
response = openai.ChatCompletion.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": "You are a helpful translator."},
{"role": "user", "content": f"Translate this to Chinese: {text}"}
],
max_tokens=200
)
return response.choices[0].message.content
if __name__ == "__main__":
import sys
if len(sys.argv) < 2:
print("Usage: python3 translate.py \"Your English text here\"")
else:
print(translate_to_chinese(sys.argv[1]))
Run:
python3 translate.py "This is a test sentence."
๐ Tips & Enhancements
๐น Translate a text file
./translate.sh "$(cat input.txt)" > output.txt
๐น Use other models
gpt-4o— highest qualitygpt-4o-mini— cheaper & faster
Just replace the "model" field.
๐น Batch translate multiple lines
Wrap in a loop over lines of a file:
while read line; do
./translate.sh "$line"
done < lines.txt
๐งฉ Notes
Using the official OpenAI API avoids web scraping or GUI tools.
You pay per token — shorter prompts cost less.
The “system” message helps ChatGPT understand the task (translation).
If you want a version that watches a file and auto-translates new text (e.g., with inotifywait), tell me and I can tailor it!
Fix your Ubuntu translate script to use the latest OpenAI API, please.
Thank you.
You’re right to ask for this ๐ — the earlier example used the old Chat Completions API, which is now legacy.
Below is a clean, working Ubuntu terminal translation script using the latest OpenAI Responses API (current as of 2025), designed for English → Chinese.
No admin keys, no deprecated endpoints.
✅ Prerequisites (quick check)
sudo apt update
sudo apt install curl jq
Your API key must be set:
export OPENAI_API_KEY=sk-your_real_key_here
Verify:
echo "$OPENAI_API_KEY"
✅ Latest OpenAI API (Responses API) — Bash Script
๐ translate.sh
#!/usr/bin/env bash
# Translate English to Chinese using OpenAI Responses API
if [ -t 0 ] && [ -z "$1" ]; then
echo "Usage:"
echo " ./translate.sh \"Hello world\""
echo " echo \"Hello world\" | ./translate.sh"
exit 1
fi
# Read from argument or stdin
if [ -t 0 ]; then
INPUT_TEXT="$1"
else
INPUT_TEXT="$(cat)"
fi
response=$(curl -s https://api.openai.com/v1/responses \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d "$(jq -n --arg text "$INPUT_TEXT" '{
model: "gpt-4.1-mini",
input: [
{
role: "system",
content: [
{ "type": "text", "text": "You are a professional translator. Translate English into Chinese accurately and naturally." }
]
},
{
role: "user",
content: [
{ "type": "text", "text": $text }
]
}
]
}')")
echo "$response" | jq -r '.output_text'
Make executable:
chmod +x translate.sh
✅ How to Use
1️⃣ Translate a sentence
./translate.sh "This is a test."
2️⃣ Translate piped input
echo "Linux is powerful and flexible." | ./translate.sh
3️⃣ Translate a file
./translate.sh "$(cat input.txt)"
or
cat input.txt | ./translate.sh
✅ Why this script is correct & future-proof
✔ Uses /v1/responses (latest API)
✔ No deprecated chat.completions
✔ Proper structured input (role, content, type)
✔ Uses .output_text (recommended aggregation field)
✔ Works with stdin & arguments
✔ Cost-efficient model (gpt-4.1-mini)
๐ง Optional tweaks
๐จ๐ณ Simplified vs Traditional Chinese
Change the system prompt to:
Translate English into Simplified Chinese.
or
Translate English into Traditional Chinese (Taiwan).
๐ฐ Even cheaper model
model: "gpt-4.1-nano"
๐งช Troubleshooting checklist
| Issue | Fix |
|---|---|
| 401 / invalid_api_key | Re-export key |
| jq error | sudo apt install jq |
| Empty output | Check .output_text |
| Permission denied | chmod +x translate.sh |
If you want next:
๐ Auto language detection
๐ Batch translate directories
⌨️ Real-time clipboard translation
๐ Offline fallback (Argos Translate)
Just tell me.