Prompt
How do I translate product reviews through an API and keep the original formatting?
Latest observation
To translate product reviews through an API and preserve the original formatting, the key is to treat the review as structured text, not just a plain string. That means you should preserve:
- line breaks
- bullet points
- emojis
- punctuation
- HTML/Markdown tags
- placeholders like
{name},%s,<a href=...>
Recommended approach
1. Use a translation API that supports formatting preservation
Some translation APIs have features for preserving tags or markup, for example:
- DeepL API: supports
tag_handling=htmlorxml - Google Cloud Translate: you may need to pre-process tags yourself
- Microsoft Translator: can also be used with text segmentation and markup handling
If your reviews are in HTML, XML, or Markdown, choose an API that can explicitly preserve tags.
2. Wrap formatting in tags or placeholders
If the review contains formatting like this:
Great product!
Pros:
- Fast
- Lightweight
Cons:
- Battery life could be better
Send it as-is if the API preserves line breaks. If not, convert it to a structured form first, such as:
<p>Great product!</p>
<p>Pros:</p>
<ul>
<li>Fast</li>
<li>Lightweight</li>
</ul>
<p>Cons:</p>
<ul>
<li>Battery life could be better</li>
</ul>
Then translate with HTML tag preservation enabled.
3. Preserve placeholders and special tokens
Before translation, protect any variable text:
{username}{product_name}%s{{count}}
Example strategy:
- Replace placeholders with temporary tokens:
{username}→__USERNAME__
- Translate the text
- Restore the original placeholders afterward
This prevents the translator from changing them.
4. Send reviews individually or in batches
For best formatting preservation:
- Translate each review separately
- Keep metadata separate from text
- Use batching only if the API clearly supports segmentation
Example JSON input:
{
"review_id": "123",
"language": "en",
"text": "Great product!\n\nPros:\n- Fast\n- Lightweight"
}
Then store the original and translated versions separately.
5. Example with DeepL-style HTML preservation
If the review is HTML:
import requests
url = "https://api.deepl.com/v2/translate"
data = {
"auth_key": "YOUR_API_KEY",
"text": "<p>Great product!</p><p>Fast shipping.</p>",
"target_lang": "DE",
"tag_handling": "html"
}
response = requests.post(url, data=data)
print(response.json())
This tells the API to preserve HTML tags while translating the text inside them.
6. If your reviews are plain text, preserve formatting manually
For plain text reviews, formatting is usually represented by:
- newlines
- spacing
- markdown syntax
You can:
- split by paragraphs
- translate paragraph by paragraph
- rejoin with the same separators
Example:
paragraphs = review.split("\n\n")
translated_paragraphs = [translate(p) for p in paragraphs]
result = "\n\n".join(translated_paragraphs)
This helps keep paragraph structure intact.
Best practices
- Do not translate HTML tags or markdown syntax
- Escape user-generated content before sending it to translation
- Validate translated output to ensure placeholders are unchanged
- Keep a copy of the original review
- Test with edge cases: emojis, URLs, URLs with query strings, mixed languages
A practical workflow
- Receive review text
- Detect/identify formatting type:
- plain text
- HTML
- Markdown
- Protect placeholders and special tokens
- Send to translation API with markup preservation enabled
- Restore placeholders if needed
- Save translated text with original formatting preserved
If you want, I can show you:
- a Python example
- a Node.js example
- or a solution for HTML/Markdown reviews specifically