01 January 2026

On Ubuntu terminal, how can English text be automatically translated into Chinese with ChatGPT AI?

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

  1. Ubuntu terminal

  2. OpenAI API Key
    If you don’t have one, create it at https://platform.openai.com/ and copy your key.

  3. Install jq (for JSON in shell)

    sudo apt update
    sudo apt install jq
    
  4. Set 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:

  1. 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]))
  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 quality

  • gpt-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

IssueFix
401 / invalid_api_keyRe-export key
jq errorsudo apt install jq
Empty outputCheck .output_text
Permission deniedchmod +x translate.sh

If you want next:

  • ๐Ÿ”„ Auto language detection

  • ๐Ÿ“ Batch translate directories

  • ⌨️ Real-time clipboard translation

  • ๐Ÿ”Œ Offline fallback (Argos Translate)

Just tell me.