From 5412ef242975bdcbdd7ccc083ed802c42649622b Mon Sep 17 00:00:00 2001 From: q-- Date: Tue, 17 Sep 2024 11:45:57 +0200 Subject: [PATCH 1/3] Make placeholder workaround more robust By using fake URLs instead of x0, x1 etc. Google Translate is much less likely to mess them up. --- src/Drivers/Translation.php | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/Drivers/Translation.php b/src/Drivers/Translation.php index f576219..d52e751 100644 --- a/src/Drivers/Translation.php +++ b/src/Drivers/Translation.php @@ -89,16 +89,30 @@ public function getGoogleTranslate($language, $token) $modifiedToken = $token; $tempStrings = []; foreach ($placeholders as $index => $placeholder) { - $tempStrings[] = 'x' . $index; + //After some experiments, I found fake URLs were most likely to be left intact by Google Translate + $tempStrings[] = 'https://t.co/' . + //Use letters instead of numbers for Newar, because Google Translate converts the numbers to Newar script + ($language === 'new' ? + mb_strtoupper(base_convert($index+10, 10, 36)) + : + //Letters break in other languages, for all other languages we'll use numbers + $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); + //In Laravel, | is used to separate pluralization variants. + //Translate each of these separately to prevent Google Translate mixing them up. + $translated = []; + foreach(explode('|', $modifiedToken) AS $translatableText){ + $translated[] = $tr->translate($translatableText); + } + $translatedText = implode('|', $translated); // Step 4: Replace the temporary unique strings back with the original placeholders - //Note: we're using case-insensitive replace because Google Translate sometimes uppercases the x + //Note: we're using case-insensitive replace because Google Translate sometimes uppercases the temp string $translatedText = str_ireplace($tempStrings, $placeholders, $translatedText); // Step 5: Check if the number of placeholders has stayed the same From fc9519fdbaa5497c7bc22e0056fbc08a65346baf Mon Sep 17 00:00:00 2001 From: q-- Date: Tue, 17 Sep 2024 11:47:33 +0200 Subject: [PATCH 2/3] Log language along with placeholder error message This makes it easier to find if you're translating multiple languages. --- src/Drivers/Translation.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Drivers/Translation.php b/src/Drivers/Translation.php index d52e751..245c78a 100644 --- a/src/Drivers/Translation.php +++ b/src/Drivers/Translation.php @@ -120,7 +120,9 @@ public function getGoogleTranslate($language, $token) if (count($translatedMatches[0]) !== count($placeholders)) { // 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", + "Warning: Placeholder count mismatch in translated text when translating %s to %s.\nOriginal text: %s\nTranslated text: %s\nExpected placeholders: %s\nActual placeholders: %s\n", + $this->sourceLanguage, + $language, $token, $translatedText, json_encode($placeholders), From 5afb0ab57f1b738b48334cdde98e8e6aaad6caf2 Mon Sep 17 00:00:00 2001 From: q-- Date: Tue, 17 Sep 2024 11:51:32 +0200 Subject: [PATCH 3/3] Inform the user of the language translated --- resources/lang/en/translation.php | 1 + src/Console/Commands/AutoTranslateKeysCommand.php | 4 ++-- src/Drivers/Translation.php | 2 ++ 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/resources/lang/en/translation.php b/resources/lang/en/translation.php index b2eb2cc..db1a8e5 100644 --- a/resources/lang/en/translation.php +++ b/resources/lang/en/translation.php @@ -17,6 +17,7 @@ 'language_key_added' => 'New language key added successfully 👏', 'no_missing_keys' => 'There are no missing translation keys in the app 🎉', 'keys_synced' => 'Missing keys synchronised successfully 🎊', + 'auto_translated_language' => 'Finished translating :language.', 'auto_translated' => 'Automated Translation completed successfully 🎊', 'search' => 'Search all translations', 'translations' => 'Translation', diff --git a/src/Console/Commands/AutoTranslateKeysCommand.php b/src/Console/Commands/AutoTranslateKeysCommand.php index e366ff2..2cf4521 100644 --- a/src/Console/Commands/AutoTranslateKeysCommand.php +++ b/src/Console/Commands/AutoTranslateKeysCommand.php @@ -37,7 +37,7 @@ public function handle() } catch (\Exception $e) { return $this->error($e->getMessage()); } - - + + } } diff --git a/src/Drivers/Translation.php b/src/Drivers/Translation.php index 245c78a..488e841 100644 --- a/src/Drivers/Translation.php +++ b/src/Drivers/Translation.php @@ -66,6 +66,8 @@ public function autoTranslate($language = false) foreach ($languages as $language => $name) { $this->saveMissingTranslations($language); $this->translateLanguage($language); + //Inform the user of what language we just finished translating + fwrite(STDOUT, __('translation::translation.auto_translated_language', ['language' => $language]) . PHP_EOL); } }