Closed AndreiMiculita closed 1 year ago
Thanks,
I've am made some changes and split code into classes. That case can be handled with custom Translator
<?php
namespace potrans\translator;
use Gettext\Translation;
class DeepLTranslator extends TranslatorAbstract {
private \DeepL\Translator $translator;
public function __construct(\DeepL\Translator $translator) {
$this->translator = $translator;
}
/**
* @throws \DeepL\DeepLException
*/
public function getTranslation(Translation $sentence): string {
$response = $this->translator->translateText(
preg_replace('/\$([^$]+)\$/i', '<keep>$1</keep>', $sentence->getOriginal()),
$this->from,
$this->to,
[
'tag_handling' => 'xml',
'ignore_tags' => 'keep',
]
);
return preg_replace('/<\/?keep>/i', '$', $response->text);
}
}
Thank you! Any chance this will be included as an optional argument in the official release? Otherwise, I'll go ahead and close this issue.
sure, after rewriting there can be optional parameter for customized translator
there is new version that allow customize translator behavior
https://github.com/OzzyCzech/potrans#custom-translator
and this specific example is now called DeepLTranslatorEscaped.php and is part of the code
let me know if you are happy with this, and if so, feel free to close this issue
Original string:
"I have $number$ apples in my $recipient$"
Desired translated string:"Tengo $number$ manzanas en mi $recipient$"
Actual translated string:"Tengo $número$ manzanas en mi $recipiente$"
The issue is the DeepL is also translating the
$number$
and$recipient$
, which I would like to specify as ignored (for example with the regex\$[^$]+\$
)Under the hood, you would search for the specified regex, add opening/closing XML tags around matching substrings with tags after receiving the translated string.
preg_replace
(such as<keep></keep>
), and specify those tags when doing the API call: https://www.deepl.com/docs-api/xml/ignored-tags/ And then remove the