{"id":234,"date":"2026-11-18T14:15:37","date_gmt":"2026-11-18T14:15:37","guid":{"rendered":"https:\/\/docs.latw.ai\/uncategorized\/checking-translations-before-save-3\/"},"modified":"2026-11-18T14:15:37","modified_gmt":"2026-11-18T14:15:37","slug":"checking-translations-before-save-3","status":"publish","type":"post","link":"https:\/\/docs.latw.ai\/pl\/latw-for-translatepress\/checking-translations-before-save-3\/","title":{"rendered":"Checking Translations Before They Are Saved"},"content":{"rendered":"<h2>What it does<\/h2>\n<p>By default a translation is saved as soon as the engine returns a well-formed answer. The plugin<br \/>\nchecks that the answer is valid JSON and that its keys match the strings that were sent \u2014 it does not<br \/>\nread the text. If the model mistranslates a term, the mistranslation goes live.<\/p>\n<p>The <code>latwaitt_translated_strings<\/code> filter is the point where your site can look at the finished<br \/>\ntranslation <em>before<\/em> it is written, and either correct it or reject it. It is part of the free<br \/>\nplugin.<\/p>\n<p>Typical uses:<\/p>\n<ul>\n<li>terminology your glossary cannot express \u2014 the glossary says &#8220;use X&#8221;, not &#8220;never use Y&#8221;;<\/li>\n<li>integrity checks, e.g. every measurement in the source must survive into the translation;<\/li>\n<li>a search-and-replace your editors keep applying by hand after every translation.<\/li>\n<\/ul>\n<p><strong>What it cannot do.<\/strong> These are text checks, not comprehension. A translation that reverses the<br \/>\nmeaning of a sentence while keeping every number, tag and glossary term intact will pass every<br \/>\ndeterministic rule you write here. Catching that needs a second AI call, which this filter lets you<br \/>\nmake, but does not make for you.<\/p>\n<h2>Where it runs<\/h2>\n<pre><code>engine answers  \u2192  JSON parsed  \u2192  latwaitt_translated_strings  \u2192  strings written into TranslatePress<\/code><\/pre>\n<p>It runs for everything translated <strong>through the plugin&#8217;s queue<\/strong> \u2014 pages from the <strong>Pages<\/strong> screen,<br \/>\npage strings and gettext from the <strong>String Translation<\/strong> screen, automatic translation, and<br \/>\ntranslations submitted from Claude or ChatGPT over MCP \u2014 with every engine and in every processing<br \/>\nmode, in the same background process that saves the translation.<\/p>\n<p>It does <strong>not<\/strong> run when this plugin is TranslatePress&#8217;s own automatic translation engine and a page<br \/>\nis translated while a visitor opens it: that answer goes straight back to TranslatePress, which saves<br \/>\nit. If every translation must pass your checks, translate through the queue and leave<br \/>\nTranslatePress&#8217;s automatic translation off.<\/p>\n<p>The filter only ever sees the strings the job sent \u2014 strings that were not translated yet. Strings<br \/>\nthat already had a translation are never part of a job. A job that turned out to have nothing left<br \/>\nto translate does not call the filter.<\/p>\n<h2>The filter<\/h2>\n<pre><code class=\"language-php\">add_filter( 'latwaitt_translated_strings', function ( $strings, $translation ) {\n    return $strings;\n}, 10, 2 );<\/code><\/pre>\n<p><code>$strings<\/code> is an array of <code>id =&gt; translated text<\/code>, where the id is TranslatePress&#8217;s own id for the<br \/>\nstring in the target language&#8217;s table.<\/p>\n<p><code>$translation<\/code> is the job&#8217;s row in the plugin&#8217;s queue:<\/p>\n<table>\n<thead>\n<tr>\n<th>Key<\/th>\n<th>Meaning<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>id<\/code><\/td>\n<td>Row id, as shown on the <strong>History<\/strong> screen.<\/td>\n<\/tr>\n<tr>\n<td><code>element_type<\/code><\/td>\n<td><code>trp_page_job<\/code> for a page, <code>trp_string_job<\/code> for page strings, <code>trp_gettext_job<\/code> for gettext.<\/td>\n<\/tr>\n<tr>\n<td><code>source_post_id<\/code><\/td>\n<td>The page for <code>trp_page_job<\/code>, <code>0<\/code> for string batches.<\/td>\n<\/tr>\n<tr>\n<td><code>source_lang<\/code><\/td>\n<td>Locale translated from, e.g. <code>en_US<\/code>.<\/td>\n<\/tr>\n<tr>\n<td><code>target_lang<\/code><\/td>\n<td>Locale translated into, e.g. <code>de_DE<\/code>.<\/td>\n<\/tr>\n<tr>\n<td><code>model<\/code>, <code>provider<\/code><\/td>\n<td>The model and engine this job was actually sent to (<code>mcp<\/code> for a translation made over MCP).<\/td>\n<\/tr>\n<tr>\n<td><code>translation_type<\/code><\/td>\n<td><code>on-demand<\/code> or <code>auto<\/code>, with <code>-delta<\/code> or <code>-delta-context<\/code> when a partly translated page was updated.<\/td>\n<\/tr>\n<tr>\n<td><code>source_strings_json<\/code><\/td>\n<td>JSON of the strings that make up the job, keyed like <code>$strings<\/code>, with the <strong>source<\/strong> text.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The source text of a string \u2014 a page job stores the text itself, a string batch stores it under<br \/>\n<code>data<\/code>:<\/p>\n<pre><code class=\"language-php\">$source = json_decode( (string) $translation['source_strings_json'], true );\n$entry  = isset( $source[ $id ] ) ? $source[ $id ] : '';\n$text   = is_array( $entry ) ? (string) $entry['data'] : (string) $entry;<\/code><\/pre>\n<h3>Returning a corrected translation<\/h3>\n<p>Return the array with whatever changes you want saved.<\/p>\n<pre><code class=\"language-php\">\/\/ Editors kept fixing this one word by hand after every translation.\nadd_filter( 'latwaitt_translated_strings', function ( $strings, $translation ) {\n    if ( $translation['target_lang'] !== 'de_DE' ) {\n        return $strings;\n    }\n\n    foreach ( $strings as $id =&gt; $translated ) {\n        $strings[ $id ] = str_replace( 'Bodensatz', 'Sediment', $translated );\n    }\n\n    return $strings;\n}, 10, 2 );<\/code><\/pre>\n<p><strong>Removing a string skips it.<\/strong> A string missing from the array is simply not written and stays<br \/>\nuntranslated in TranslatePress, to be translated another time. Strings you add are ignored \u2014 only<br \/>\nthe strings the job asked about are ever written.<\/p>\n<h3>Rejecting a translation<\/h3>\n<p>Return a <code>WP_Error<\/code> \u2014 or an empty array \u2014 and nothing is saved. The job is marked <strong>failed<\/strong>, its<br \/>\nstrings stay untranslated, and the reason is stored on the job and written to the log.<\/p>\n<p>Anything other than an array is treated as a rejection too, so always return <code>$strings<\/code> from a<br \/>\nbranch that does not apply.<\/p>\n<pre><code class=\"language-php\">\/\/ Words that must never appear, per language.\nadd_filter( 'latwaitt_translated_strings', function ( $strings, $translation ) {\n    $banned = array(\n        'de_DE' =&gt; array( '\/bBodensatzb\/u' ),\n        'it_IT' =&gt; array( '\/bspremiagrumib\/iu' ),\n    );\n\n    if ( empty( $banned[ $translation['target_lang'] ] ) ) {\n        return $strings;\n    }\n\n    foreach ( $strings as $id =&gt; $translated ) {\n        foreach ( $banned[ $translation['target_lang'] ] as $pattern ) {\n            if ( preg_match( $pattern, $translated ) ) {\n                return new WP_Error(\n                    'mysite_banned_term',\n                    sprintf( 'Banned term in string %s (%s)', $id, $pattern )\n                );\n            }\n        }\n    }\n\n    return $strings;\n}, 10, 2 );<\/code><\/pre>\n<pre><code class=\"language-php\">\/\/ Every measurement in the source must appear in the translation.\nadd_filter( 'latwaitt_translated_strings', function ( $strings, $translation ) {\n    $source      = json_decode( (string) $translation['source_strings_json'], true );\n    $measurement = '\/d+(?:[.,]d+)?s?(?:mm|cm|m|ml|l|g|kg|%)b\/u';\n\n    foreach ( $strings as $id =&gt; $translated ) {\n        $entry = isset( $source[ $id ] ) ? $source[ $id ] : '';\n        $text  = is_array( $entry ) ? (string) $entry['data'] : (string) $entry;\n\n        preg_match_all( $measurement, wp_strip_all_tags( $text ), $in_source );\n        preg_match_all( $measurement, wp_strip_all_tags( $translated ), $in_translation );\n\n        if ( count( $in_source[0] ) !== count( $in_translation[0] ) ) {\n            return new WP_Error(\n                'mysite_measurements_lost',\n                sprintf(\n                    'String %s: %d measurements in the source, %d in the translation.',\n                    $id,\n                    count( $in_source[0] ),\n                    count( $in_translation[0] )\n                )\n            );\n        }\n    }\n\n    return $strings;\n}, 10, 2 );<\/code><\/pre>\n<p><strong>A rejected job is not retried.<\/strong> Its strings stay untranslated until someone translates them again, and<br \/>\nthat sends the same prompt again: nothing tells the model what you objected to, so a model that made<br \/>\na deliberate word choice will usually make it again \u2014 at your provider&#8217;s expense. Rejection is best<br \/>\nused for mistakes a model makes <em>occasionally<\/em> (a dropped number, a truncated string). To drop only<br \/>\nthe offending string rather than the whole job, remove it from the array instead. For a word the<br \/>\nmodel keeps choosing, correct it in the filter, add the preferred term to the glossary, or say so in<br \/>\nthe translation prompt.<\/p>\n<h2>Where to check on it<\/h2>\n<ul>\n<li><strong>AI Translation \u2192 History<\/strong> shows the job as <code>failed<\/code>, with your message in the <strong>Error<\/strong> column.<\/li>\n<li>The plugin log (<strong>AI Translation \u2192 Logs<\/strong>) records <code>Response processing failed<\/code> with the error<br \/>\nmessage.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>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\u2026<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_latwm_translated_slug":"","footnotes":""},"categories":[19],"tags":[],"class_list":["post-234","post","type-post","status-publish","format-standard","hentry","category-latw-for-translatepress"],"_links":{"self":[{"href":"https:\/\/docs.latw.ai\/pl\/wp-json\/wp\/v2\/posts\/234","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/docs.latw.ai\/pl\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.latw.ai\/pl\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.latw.ai\/pl\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.latw.ai\/pl\/wp-json\/wp\/v2\/comments?post=234"}],"version-history":[{"count":0,"href":"https:\/\/docs.latw.ai\/pl\/wp-json\/wp\/v2\/posts\/234\/revisions"}],"wp:attachment":[{"href":"https:\/\/docs.latw.ai\/pl\/wp-json\/wp\/v2\/media?parent=234"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.latw.ai\/pl\/wp-json\/wp\/v2\/categories?post=234"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.latw.ai\/pl\/wp-json\/wp\/v2\/tags?post=234"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}