From b7e0bc9bd383bacce679ac38c757791ddc358671 Mon Sep 17 00:00:00 2001 From: q-- <2470175+q--@users.noreply.github.com> Date: Tue, 2 Jul 2024 16:54:06 +0200 Subject: [PATCH 1/2] Work around temp strings being uppercased Handle Google Translate sometimes turning x0, x1 etc. into X0, X1 in the translated string. --- src/Drivers/Translation.php | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/Drivers/Translation.php b/src/Drivers/Translation.php index e237f63..6b5b731 100644 --- a/src/Drivers/Translation.php +++ b/src/Drivers/Translation.php @@ -87,21 +87,19 @@ public function getGoogleTranslate($language, $token) // Step 2: Replace placeholders with temporary unique strings $modifiedToken = $token; - $placeholderMap = []; + $tempStrings = []; foreach ($placeholders as $index => $placeholder) { - $tempString = 'x' . $index; - $placeholderMap[$tempString] = $placeholder; - $modifiedToken = str_replace($placeholder, $tempString, $modifiedToken); + $tempStrings[] = 'x' . $index; } + $modifiedToken = str_replace($placeholders, $tempStrings, $modifiedToken); // Step 3: Translate the modified text using Google Translate $tr = new GoogleTranslate($language, $this->sourceLanguage); $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); - } + //Note: we're using case-insensitive replace because Google Translate sometimes uppercases the x + $translatedText = str_ireplace($tempStrings, $placeholders, $translatedText); // Step 5: Check if the number of placeholders has stayed the same preg_match_all($placeholderRegex, $translatedText, $translatedMatches); From da3cbd86707e92d369a31a31419c432ece4e3a96 Mon Sep 17 00:00:00 2001 From: q-- <2470175+q--@users.noreply.github.com> Date: Tue, 2 Jul 2024 16:57:06 +0200 Subject: [PATCH 2/2] Don't exit process on missing placeholder That's a bit too rigorous for something that can be fixed manually. Now it logs an error instead. --- src/Drivers/Translation.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Drivers/Translation.php b/src/Drivers/Translation.php index 6b5b731..f576219 100644 --- a/src/Drivers/Translation.php +++ b/src/Drivers/Translation.php @@ -104,9 +104,9 @@ public function getGoogleTranslate($language, $token) // 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", + // Print a warning to stderr + fwrite(STDERR, sprintf( + "Warning: Placeholder count mismatch in translated text.\nOriginal text: %s\nTranslated text: %s\nExpected placeholders: %s\nActual placeholders: %s\n", $token, $translatedText, json_encode($placeholders),