OzzyCzech / potrans

Command line tool for translate Gettext with Google Translator API or DeepL API
MIT License
90 stars 30 forks source link

Add option for DeepL to ignore certain substrings matched by regex #16

Closed AndreiMiculita closed 1 year ago

AndreiMiculita commented 1 year ago

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 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 tags after receiving the translated string.

OzzyCzech commented 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);
    }
}
AndreiMiculita commented 1 year ago

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.

OzzyCzech commented 1 year ago

sure, after rewriting there can be optional parameter for customized translator

OzzyCzech commented 1 year ago

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

OzzyCzech commented 1 year ago

let me know if you are happy with this, and if so, feel free to close this issue