Skip to content

Commit

Permalink
Merge pull request #16 from q--/Make_placeholder_workaround_less_like…
Browse files Browse the repository at this point in the history
…ly_to_be_mangled_by_Google_Translate

Make placeholder workaround less likely to be mangled by Google Translate
  • Loading branch information
q-- authored Sep 17, 2024
2 parents d317bfd + 5afb0ab commit 1f7eb1d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
1 change: 1 addition & 0 deletions resources/lang/en/translation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
4 changes: 2 additions & 2 deletions src/Console/Commands/AutoTranslateKeysCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function handle()
} catch (\Exception $e) {
return $this->error($e->getMessage());
}


}
}
26 changes: 22 additions & 4 deletions src/Drivers/Translation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -89,24 +91,40 @@ 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
preg_match_all($placeholderRegex, $translatedText, $translatedMatches);
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),
Expand Down

0 comments on commit 1f7eb1d

Please sign in to comment.