Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make placeholder workaround less likely to be mangled by Google Translate #16

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading