From 43c4fa7de1c466fb3167d06ba2bde60060aee02f Mon Sep 17 00:00:00 2001 From: q-- <2470175+q--@users.noreply.github.com> Date: Mon, 1 Jul 2024 17:26:53 +0200 Subject: [PATCH] Prevent placeholders from being auto-translated - Replace placeholders with temporary unique strings (e.g., x0, x1, etc.) before sending to Google Translate - Restore original placeholders after translation --- src/Drivers/Translation.php | 44 ++++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/src/Drivers/Translation.php b/src/Drivers/Translation.php index c8e1a19..e237f63 100644 --- a/src/Drivers/Translation.php +++ b/src/Drivers/Translation.php @@ -77,9 +77,46 @@ public function autoTranslate($language = false) * @return string|null * @throws \ErrorException */ - public function getGoogleTranslate($language,$token){ + public function getGoogleTranslate($language, $token) + { + $placeholderRegex = '/:([a-zA-Z0-9_]+)/'; + + // Step 1: Identify placeholders + preg_match_all($placeholderRegex, $token, $matches); + $placeholders = $matches[0]; + + // Step 2: Replace placeholders with temporary unique strings + $modifiedToken = $token; + $placeholderMap = []; + foreach ($placeholders as $index => $placeholder) { + $tempString = 'x' . $index; + $placeholderMap[$tempString] = $placeholder; + $modifiedToken = str_replace($placeholder, $tempString, $modifiedToken); + } + + // Step 3: Translate the modified text using Google Translate $tr = new GoogleTranslate($language, $this->sourceLanguage); - return $tr->translate($token); + $translatedText = $tr->translate($modifiedToken); + + // Step 4: Replace the temporary unique strings back with the original placeholders + foreach ($placeholderMap as $tempString => $placeholder) { + $translatedText = str_replace($tempString, $placeholder, $translatedText); + } + + // Step 5: Check if the number of placeholders has stayed the same + preg_match_all($placeholderRegex, $translatedText, $translatedMatches); + if (count($translatedMatches[0]) !== count($placeholders)) { + // Display an error or warning with more details + throw new \ErrorException(sprintf( + "Placeholder count mismatch in translated text.\nOriginal text: %s\nTranslated text: %s\nExpected placeholders: %s\nActual placeholders: %s", + $token, + $translatedText, + json_encode($placeholders), + json_encode($translatedMatches[0]) + )); + } + + return $translatedText; } /** @@ -87,7 +124,8 @@ public function getGoogleTranslate($language,$token){ * * @param $language */ - public function translateLanguage($language){ + public function translateLanguage($language) + { //No need to translate e.g. English to English if ($language === $this->sourceLanguage) { return;