Added Automatic Bilingual Support to the Website
Implementing automatic Chinese-English bilingual support for a blog with SenseNova: AI-powered body translation, lookup table for fixed text, translation cache reuse, SEO support, and graceful fallback.
Built with SenseNova models, the blog automatically translates content in the background within 20 seconds of publishing.
The feature is straightforward. Here's the general logic:
1. One URL for Chinese, One for English
The Chinese version keeps the original URL; the English version simply adds /en to the front. Both addresses share the same page, so the layout and styling are identical.
2. Check the Visitor's Language on Entry
Every time someone opens the site, the system checks their browser language:
- Chinese browser → stays on the Chinese site;
- English or any other language → automatically redirected to
/en.
If a visitor has manually clicked the "EN / 中文" toggle in the top-right corner, the system remembers that choice and won't override it next time. Search engine crawlers are never forcibly redirected, to avoid indexing errors.
3. Static Text Uses a Pre-Built Lookup Table
Navigation labels, buttons, "No articles yet," comment box placeholders — all these fixed strings are pre-written in both Chinese and English. The system just looks them up directly, no AI needed.
4. Article Content Goes Through AI Translation, But Gets "Packaged and Protected" First
Code blocks, links, images, URLs, and other elements that must not be altered are replaced with placeholder tags before translation. The AI only translates the actual text. After translation, the placeholders are restored to their original positions.
This means the English version of an article has the exact same formatting, code, links, images, and table of contents as the Chinese version — nothing gets scrambled.
5. Translate Once, Cache Forever — No Repeated Costs
Translated content is stored in the database. The next visitor just pulls it from cache — no re-translation needed.
If an article is updated, the system detects the change and re-translates it.
List pages never wait for translation — they load instantly.
If an article hasn't been translated yet when someone views it for the first time, the system waits up to 20 seconds. If it's still not ready, the visitor sees the Chinese version with a notice saying "English version is being generated."
A background job runs every 5 minutes to batch-translate any remaining untranslated content.
6. Graceful Fallback — No Broken Pages
If the AI key isn't configured, the network times out, or the model misbehaves, the page simply falls back to Chinese. No blank screens, no error messages.
7. Search Engines Can Tell the Two Versions Apart
The Chinese and English pages exchange hreflang signals, telling search engines "we're two language versions of the same article." Each version gets indexed independently and correctly.
Published Oct 18 ·
Leave a note
No comments yet — be the first.