Skip to content
LATW AI Translation for TranslatePress

Checking Translations Before They Are Saved

On this page

#What it does

By default a translation is saved as soon as the engine returns a well-formed answer. The plugin
checks that the answer is valid JSON and that its keys match the strings that were sent — it does not
read the text. If the model mistranslates a term, the mistranslation goes live.

The latwaitt_translated_strings filter is the point where your site can look at the finished
translation before it is written, and either correct it or reject it. It is part of the free
plugin.

Typical uses:

  • terminology your glossary cannot express — the glossary says “use X”, not “never use Y”;
  • integrity checks, e.g. every measurement in the source must survive into the translation;
  • a search-and-replace your editors keep applying by hand after every translation.

What it cannot do. These are text checks, not comprehension. A translation that reverses the
meaning of a sentence while keeping every number, tag and glossary term intact will pass every
deterministic rule you write here. Catching that needs a second AI call, which this filter lets you
make, but does not make for you.

#Where it runs

engine answers  →  JSON parsed  →  latwaitt_translated_strings  →  strings written into TranslatePress

It runs for everything translated through the plugin’s queue — pages from the Pages screen,
page strings and gettext from the String Translation screen, automatic translation, and
translations submitted from Claude or ChatGPT over MCP — with every engine and in every processing
mode, in the same background process that saves the translation.

It does not run when this plugin is TranslatePress’s own automatic translation engine and a page
is translated while a visitor opens it: that answer goes straight back to TranslatePress, which saves
it. If every translation must pass your checks, translate through the queue and leave
TranslatePress’s automatic translation off.

The filter only ever sees the strings the job sent — strings that were not translated yet. Strings
that already had a translation are never part of a job. A job that turned out to have nothing left
to translate does not call the filter.

#The filter

add_filter( 'latwaitt_translated_strings', function ( $strings, $translation ) {
    return $strings;
}, 10, 2 );

$strings is an array of id => translated text, where the id is TranslatePress’s own id for the
string in the target language’s table.

$translation is the job’s row in the plugin’s queue:

Key Meaning
id Row id, as shown on the History screen.
element_type trp_page_job for a page, trp_string_job for page strings, trp_gettext_job for gettext.
source_post_id The page for trp_page_job, 0 for string batches.
source_lang Locale translated from, e.g. en_US.
target_lang Locale translated into, e.g. de_DE.
model, provider The model and engine this job was actually sent to (mcp for a translation made over MCP).
translation_type on-demand or auto, with -delta or -delta-context when a partly translated page was updated.
source_strings_json JSON of the strings that make up the job, keyed like $strings, with the source text.

The source text of a string — a page job stores the text itself, a string batch stores it under
data:

$source = json_decode( (string) $translation['source_strings_json'], true );
$entry  = isset( $source[ $id ] ) ? $source[ $id ] : '';
$text   = is_array( $entry ) ? (string) $entry['data'] : (string) $entry;

#Returning a corrected translation

Return the array with whatever changes you want saved.

// Editors kept fixing this one word by hand after every translation.
add_filter( 'latwaitt_translated_strings', function ( $strings, $translation ) {
    if ( $translation['target_lang'] !== 'de_DE' ) {
        return $strings;
    }

    foreach ( $strings as $id => $translated ) {
        $strings[ $id ] = str_replace( 'Bodensatz', 'Sediment', $translated );
    }

    return $strings;
}, 10, 2 );

Removing a string skips it. A string missing from the array is simply not written and stays
untranslated in TranslatePress, to be translated another time. Strings you add are ignored — only
the strings the job asked about are ever written.

#Rejecting a translation

Return a WP_Error — or an empty array — and nothing is saved. The job is marked failed, its
strings stay untranslated, and the reason is stored on the job and written to the log.

Anything other than an array is treated as a rejection too, so always return $strings from a
branch that does not apply.

// Words that must never appear, per language.
add_filter( 'latwaitt_translated_strings', function ( $strings, $translation ) {
    $banned = array(
        'de_DE' => array( '/bBodensatzb/u' ),
        'it_IT' => array( '/bspremiagrumib/iu' ),
    );

    if ( empty( $banned[ $translation['target_lang'] ] ) ) {
        return $strings;
    }

    foreach ( $strings as $id => $translated ) {
        foreach ( $banned[ $translation['target_lang'] ] as $pattern ) {
            if ( preg_match( $pattern, $translated ) ) {
                return new WP_Error(
                    'mysite_banned_term',
                    sprintf( 'Banned term in string %s (%s)', $id, $pattern )
                );
            }
        }
    }

    return $strings;
}, 10, 2 );
// Every measurement in the source must appear in the translation.
add_filter( 'latwaitt_translated_strings', function ( $strings, $translation ) {
    $source      = json_decode( (string) $translation['source_strings_json'], true );
    $measurement = '/d+(?:[.,]d+)?s?(?:mm|cm|m|ml|l|g|kg|%)b/u';

    foreach ( $strings as $id => $translated ) {
        $entry = isset( $source[ $id ] ) ? $source[ $id ] : '';
        $text  = is_array( $entry ) ? (string) $entry['data'] : (string) $entry;

        preg_match_all( $measurement, wp_strip_all_tags( $text ), $in_source );
        preg_match_all( $measurement, wp_strip_all_tags( $translated ), $in_translation );

        if ( count( $in_source[0] ) !== count( $in_translation[0] ) ) {
            return new WP_Error(
                'mysite_measurements_lost',
                sprintf(
                    'String %s: %d measurements in the source, %d in the translation.',
                    $id,
                    count( $in_source[0] ),
                    count( $in_translation[0] )
                )
            );
        }
    }

    return $strings;
}, 10, 2 );

A rejected job is not retried. Its strings stay untranslated until someone translates them again, and
that sends the same prompt again: nothing tells the model what you objected to, so a model that made
a deliberate word choice will usually make it again — at your provider’s expense. Rejection is best
used for mistakes a model makes occasionally (a dropped number, a truncated string). To drop only
the offending string rather than the whole job, remove it from the array instead. For a word the
model keeps choosing, correct it in the filter, add the preferred term to the glossary, or say so in
the translation prompt.

#Where to check on it

  • AI Translation → History shows the job as failed, with your message in the Error column.
  • The plugin log (AI Translation → Logs) records Response processing failed with the error
    message.