PHP code example of statikbe / craft-deepl

1. Go to this page and download the library: Download statikbe/craft-deepl library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

statikbe / craft-deepl example snippets




return [
    'glossaries' => [
        [
            'name' => 'Glossary 1',
            'source' => 'nl',
            'target' => 'fr',
            'entries' => [
                "Word NL" => "Word FR",
            ]
        ]
    ]
];

use DeepL\TranslateTextOptions;
use statikbe\deepl\events\ModifyTranslateOptionsEvent;
use statikbe\deepl\services\ApiService;
use yii\base\Event;

Event::on(
    ApiService::class,
    ApiService::EVENT_BEFORE_TRANSLATE,
    function (ModifyTranslateOptionsEvent $event) {
        if ($event->targetLang === 'fr') {
            $event->customInstructions[] = 'Use a friendly, informal tone (tutoiement).';
            $event->customInstructions[] = 'Translate brand names literally; do not localize.';
        }

        // Or set any other DeepL option directly:
        $event->options[TranslateTextOptions::FORMALITY] = 'less';
    }
);

Event::on(
    ApiService::class,
    ApiService::EVENT_BEFORE_TRANSLATE,
    function (ModifyTranslateOptionsEvent $event) {
        match ($event->targetSite?->handle) {
            'belgiumFr' => $event->customInstructions[] = 'Use Belgian-French terminology (mutuelle, numéro national).',
            'franceFr'  => $event->customInstructions[] = 'Use Metropolitan French terminology (sécurité sociale).',
            default     => null,
        };
    }
);

Event::on(
    ApiService::class,
    ApiService::EVENT_BEFORE_TRANSLATE,
    function (ModifyTranslateOptionsEvent $event) {
        $entryId = Craft::$app->getRequest()->getParam('entryId');
        $entry = $entryId ? Craft::$app->getEntries()->getEntryById((int) $entryId) : null;

        if ($entry?->section?->handle === 'legal') {
            $event->customInstructions[] = 'Preserve all legal terminology verbatim; do not paraphrase.';
            $event->options[TranslateTextOptions::FORMALITY] = 'more';
        }
    }
);
`bash
php craft deepl/glossary/sync