Translate a WordPress site without duplicating pages
You can translate a WordPress site without keeping a translated copy of every post by rewriting each page after WordPress has finished building it. The plugin catches the finished HTML, sends the visible text to a translation provider, remembers every result, and serves the translated page at the same address with ?lang= added. I built one that works this way, called “Alcor Translate” in the WordPress admin, and this write-up covers how it is built, how it fails and what has not been measured.
Read it as a description of code, not a demo. I have no translated output, timings or quality measurements to publish for this plugin, so everything below is what the source does.
The problem
One common way to make a WordPress site multilingual is to keep a translated copy of each post and page. The cost arrives later. Change an English headline and every translated copy needs the same change. The copies also cover only the content you own: theme text, plugin output, navigation labels, image descriptions and meta descriptions all live outside the post body, so each one needs its own integration.
The other route is to call a translation service on every page view, which adds a network call to each request and repeats work already done. This plugin sits between the two. It translates on demand, but remembers every string so the same text is never translated twice.
What it does
- Works on any front-end page. It hooks template_redirect, which runs for every front-end view, and skips feeds, REST requests and any output that is not a full HTML page.
- Picks the language from the address or a cookie. It accepts ten targets, checked against an allowlist: Spanish, French, German, Portuguese, Italian, Japanese, Korean, Chinese, Hindi and Arabic. ?lang=en shows the original page.
- Translates text and a few attributes. That means page text, the title, the meta description, and image alt, placeholder and aria-label text. Text directly inside script, style, code, pre, noscript and textarea elements is skipped, and fragments without a run of two letters, such as punctuation and numbers, are never sent.
- Labels a page only when it was translated. If at least one string changed, it sets lang on the html element, and dir=”rtl” for Arabic. If nothing changed, the original page goes out byte for byte, still labeled English.
- Advertises only what exists. The first time it serves a page with translated text in a language, it writes a small marker file. The hreflang alternate links are built from those markers, so a page gets an alternate link for a language only after it has been served in that language. If a later view translates nothing, the marker is removed.
- Remembers the choice. A site-wide cookie lasts 30 days and is written only after a translated page has been served.
- Has a settings page under Settings, then “Alcor Translate”: on or off, provider, endpoint, API key and a checkbox for each language, all sanitized when saved.
How it works
The plugin is one PHP file of 339 lines. It needs nothing beyond WordPress core and PHP’s DOM extension.
An output buffer is PHP’s way of holding a page’s output in memory until the page is complete, so code can change it before it is sent. On plugins_loaded the plugin works out the language from the query string or cookie, before any output exists. On template_redirect, at priority 1, it starts a buffer, but only for requests that asked for a language. When the page has finished rendering, the buffer callback loads the HTML into DOMDocument, collects the text with XPath, translates it as one batch and writes the results back into the same nodes.
Each string is cached as its own file under wp-content/uploads/alcor-translate/, in a folder per language, named for the MD5 hash of the source text. Because the key is the text itself, a navigation label or footer line is translated once for the whole site and reused wherever it appears. Edited English text gets a new key and is translated afresh.
Uncached strings are capped at 80 per request. The rest stay English on that view and are filled in on later views. That bounds the provider work for any one page, but it is still a blocking HTTP call with a 25 to 30 second timeout: 25 seconds for DeepL and Google, 30 for LibreTranslate. I have not measured what that costs in wall-clock time.
Three providers sit behind one function: LibreTranslate against an endpoint you set, which is the default and can be self-hosted, then DeepL, then Google Translate v2. The LibreTranslate call sends one string, so a batch is joined with a separator token and split again on the way back. If the segment count does not match, the whole batch is discarded rather than mis-assigned.
Failure is the important design point. Every output slot starts as the original English text and is overwritten only when the provider returns a well-formed answer. A dead endpoint, a bad key or an unexpected response produces the English page, not an error and not a blank region. When nothing at all was translated, the plugin returns the original HTML unchanged, skips the cookie and the language labels, and removes that page’s marker so no alternate link is advertised.
There is no second copy of any post, no translation post type and no database table. State is one options row, the cache files and the marker files. The plugin has no uninstall routine, so that row and those files stay until you remove them.
What it does not do
- It is version 1.0.1. I have no output samples, speed figures, cost per page or quality measurements to publish. Machine translation is not a substitute for a human translator on copy that matters.
- Which provider an install uses, and its key, are settings in the database. The default is LibreTranslate at the endpoint you configure. The DeepL and Google addresses are fixed in the code.
- The floating language switcher is in the code but switched off. Visitors reach a language by address or cookie unless the theme adds a switcher.
- Nothing keeps language versions apart in a page cache. The plugin sends no Vary header, so a full-page cache or CDN in front of it needs its own rule for the ?lang= parameter and the language cookie, or one visitor’s language can be served to another.
- The cache is never pruned, and a poor translation stays cached until someone deletes its file.
- English is the fixed source language, and the target list is fixed at ten languages.
- Nothing in the code handles inline SVG or HTML5-only markup specially. PHP’s HTML parser predates HTML5, so I would test a complex page before trusting it.
One naming note. AlcorTranslate, the free conversation translator that runs in Chrome on a desktop, is separate software from this plugin. The other free tools are on the apps page.
If you want something like this
If your WordPress site needs translation, or another behavior built in as a plugin, tell me what the site has to do. You get a written quote before any work starts, and the code and site I deliver are yours. See what we build for small businesses, or get a written quote.