Prompt
How do I translate database records with an API?
Latest observation
To translate database records through an API, the usual pattern is:
- Read records from your database
- Send the text fields to a translation API
- Store or return the translated text
- Optionally cache translations so you don’t translate the same text repeatedly
Typical flow
1) Fetch the records
Example: you have products with names/descriptions in English.
[
{ "id": 1, "name": "Red chair", "description": "Comfortable wooden chair" },
{ "id": 2, "name": "Blue lamp", "description": "Modern desk lamp" }
]
2) Call a translation API
Send the text to something like:
- Google Cloud Translation API
- DeepL API
- Microsoft Translator
- OpenAI API, if you want more flexible translation behavior
Example request shape:
{
"q": ["Red chair", "Comfortable wooden chair"],
"source": "en",
"target": "es"
}
3) Map translations back to records
The API typically returns translated strings in the same order:
[
"Silla roja",
"Silla de madera cómoda"
]
Then update your records:
[
{ "id": 1, "name_es": "Silla roja", "description_es": "Silla de madera cómoda" }
]
Example in pseudocode
records = db.fetch("SELECT id, name, description FROM products WHERE language='en'")
texts = []
for r in records:
texts.append(r["name"])
texts.append(r["description"])
translations = translate_api(texts, source="en", target="es")
i = 0
for r in records:
r["name_es"] = translations[i]
r["description_es"] = translations[i+1]
i += 2
db.update("products", r)
Best practices
Batch requests
Translate many strings at once instead of one-by-one to reduce cost and latency.
Store translated columns separately
Common options:
- Add columns like
name_es,description_es - Or use a separate translations table:
record_idfield_namelanguage_codetranslated_text
Cache translations
If the same text appears often, store the translation result and reuse it.
Handle formatting carefully
If text contains:
- HTML
- Markdown
- placeholders like
{name}or%s
Make sure the translation API preserves them or you protect them first.
Use fallback logic
If a translation is missing:
- show original text
- or fallback to a default language
Suggested database design for many languages
A translation table is often cleaner than adding many columns:
products
- id
- sku
- price
product_translations
- id
- product_id
- field_name
- language_code
- translated_text
Example rows:
| product_id | field_name | language_code | translated_text |
|---|---|---|---|
| 1 | name | es | Silla roja |
| 1 | description | es | Silla de madera cómoda |
If you want live translation
You can also translate on demand:
- user requests a language
- API checks if translation exists in DB
- if not, calls translation service
- stores result
- returns translated text
This is usually the best balance of speed and cost.
If you want, I can show you:
- a Python example
- a Node.js example
- a SQL schema for multilingual records
- or how to do this with OpenAI / DeepL / Google Translate specifically