Sawir Live Translate

Plugin Banner

Sawir Live Translate

by Ricardo Sawir

Download
Description

Open any page on your site, click “Translate this page” in the admin bar, choose a
target language, and either auto-translate every visible string (via a free
machine-translation service) or edit translations by hand while seeing the page
update live.

Saved translations are rendered server-side into the HTML response (SEO-friendly:
no flash of untranslated text, and hreflang alternates in the head), and visitors
switch languages via a link-based switcher (bottom-right).

Developed by Ricardo Sawir for Sawir Studio LLC.

  • Website: https://sawirstudio.com
  • X: https://x.com/RicardoSawir
  • GitHub: https://github.com/sawirricardo
  • Email: ricardo@sawirstudio.com

URLs use a language path prefix, e.g. /es/about for Spanish, /about for the source
language (which stays canonical, un-prefixed). Language is resolved in this order:
/es/ path ?lang=es saved cookie browser Accept-Language source. Root and
prefix-less URLs auto-detect the visitor’s browser language without redirecting
(a Vary header is sent so shared caches behave).

  1. Settings Live Translate: set your source language and tick the languages to offer.
  2. Visit a front-end page and click “🌐 Translate this page” in the admin bar.
  3. Pick a language, hit “Auto-translate empty” (or edit strings), then “Save”.
  4. Visitors use the switcher (bottom-right); each language has its own /xx/ URL.

Note: language path prefixes assume a root-domain install and no top-level page
whose slug is a two-letter language code.

External services

This plugin’s optional “Auto-translate” feature (admin-only, triggered manually
by clicking an Auto-translate/Translate button) sends the source text strings
and the chosen language pair to the MyMemory translation API
(https://mymemory.translated.net/) to fetch machine translations. The request
is made from the administrator’s browser only when they click the button; no
visitor data is ever sent, and nothing is sent automatically.

  • Service terms: https://mymemory.translated.net/doc/en/tos.php
  • Privacy policy: https://mymemory.translated.net/doc/en/privacy.php

Developers can swap the provider — see “Custom translation provider” below.

Native WordPress localization (dates, core/theme strings)

On a translated request the plugin switches WordPress’s locale to the target
language, so dates (month/day names) and any installed core, theme, and plugin
translations render natively — the string map only covers the rest.

For this to have visible effect the language pack must be installed. The easiest
way is Settings General Site Language: pick and save the language once (which
downloads its pack), then switch back to your source language. Languages without
an installed pack still work for content translation; only native dates/strings
are skipped. Map codes to custom WP locales with the ‘sawir_lt_wp_locale’ filter.

Scan site for strings

Settings Live Translate “Scan site now” crawls your published pages, posts,
and public archives to discover every translatable string up front (batched, with
a progress indicator). Discovered strings then appear in the “Edit all site
strings” section right below, where you can pick a language and translate, auto-
translate, or bulk-import the whole site at once instead of page by page. Note:
machine translation is still rate-limited by the free service, so for large sites
use Bulk edit to export and translate elsewhere.

Multisite

Multisite-aware. Each subsite has its own settings and its own translations table,
configured independently. Tables are created on network activation, when a new
subsite is created, and lazily on first admin visit; uninstall cleans every site
on the network.

Custom switcher

Turn off “Built-in switcher” in settings and render your own anywhere in your
theme using sawir_lt_switcher_links(), which returns the language links for the
current page:

<?php if ( function_exists( 'sawir_lt_switcher_links' ) ) : ?>
  <ul class="my-lang-switcher">
  <?php foreach ( sawir_lt_switcher_links() as $l ) : ?>
    <li>
      <a href="<?php echo esc_url( $l['url'] ); ?>"<?php echo $l['active'] ? ' aria-current="true"' : ''; ?>>
        <?php echo esc_html( $l['name'] ); ?>
      </a>
    </li>
  <?php endforeach; ?>
  </ul>
<?php endif; ?>

Each item is [‘code’,’name’,’url’,’active’,’is_source’] (code is ” for the
source language). sawir_lt_current_lang() returns the active language code.

Custom translation provider

Auto-translate uses the free MyMemory API by default. To use your own provider
(DeepL, Google, a local model, …), reassign window.SawirLT.translate from a
script enqueued after the ‘sawir-lt-walk’ handle. Signature: it receives the
source text and the two language codes, and returns a Promise resolving to the
translated string. Every auto-translate path (in-page editor and admin bulk
translate) routes through it.

add_action( 'admin_enqueue_scripts', 'myprefix_lt_provider' );
add_action( 'wp_enqueue_scripts', 'myprefix_lt_provider' );
function myprefix_lt_provider() {
    if ( wp_script_is( 'sawir-lt-walk', 'registered' ) ) {
        wp_enqueue_script( 'myprefix-lt-provider',
            plugins_url( 'provider.js', __FILE__ ),
            array( 'sawir-lt-walk' ), '1.0.0', true );
    }
}

provider.js:

window.SawirLT.translate = function ( text, source, target ) {
    return fetch( 'https://example.com/translate', {
        method: 'POST',
        body: JSON.stringify( { text: text, from: source, to: target } )
    } ).then( function ( r ) { return r.json(); } )
      .then( function ( j ) { return j.translation; } );
};<h3>Translations</h3>

The plugin’s own admin/UI text is translation-ready (text domain
“sawir-live-translate”). A .pot template ships in /languages — drop your
.po/.mo there or under wp-content/languages/plugins/. (The in-page editor’s
JavaScript labels are not yet localized.)

Other plugins by Sawir Studio

1.0.0

  • Initial release.
Back to top