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 f576219..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); } } @@ -89,16 +91,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 @@ -106,7 +122,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),