Zum Inhalt

Example Plugins

Example Plugins

BabelTranslate invokes two Plugin events that can be used to extend the resource translation. You have to create a plugin with PHP code and assign one (or two) of the BabelTranslate events to this event.

OnBabelTranslateResource

The following code contains a plugin that translates the SeoSuite 3 keywords of a resource.

translateresourceseosuite.plugin.php
<?php
/**
 * BabelTranslateResourceSeosuite Plugin
 *
 * @var modX $modx
 * @var array $scriptProperties
 */

use Sterc\SeoSuite\SeoSuite;
use Sterc\SeoSuite\Model\SeoSuiteResource;
use TreehillStudio\BabelTranslate\Translator\TranslatorInterface;

/** @var TranslatorInterface $translator */
$translator = $modx->getOption('translator', $scriptProperties);
/** @var modResource $from */
$from = $modx->getOption('from', $scriptProperties);
/** @var string $fromCulture */
$fromCulture = $modx->getOption('fromCulture', $scriptProperties);
/** @var modResource $to */
$to = $modx->getOption('to', $scriptProperties);
/** @var string $toCulture */
$toCulture = $modx->getOption('toCulture', $scriptProperties);

$corePath = $modx->getOption('babeltranslate.core_path', null, $modx->getOption('core_path') . 'components/babeltranslate/');
/** @var BabelTranslate $babeltranslate */
$babeltranslate = $modx->getService('babeltranslate', 'BabelTranslate', $corePath . 'model/babeltranslate/', [
    'core_path' => $corePath
]);

/** @var SeoSuite $seoSuite */
$seoSuite = $modx->services->get('seosuite');

// Check if the SeoSuite service is loaded
if ($seoSuite) {
    /** @var SeoSuiteResource $fromSeosuite */
    $fromSeosuite = $modx->getObject(SeoSuiteResource::class, ['resource_id' => $from->get('id')]);
    /** @var SeoSuiteResource $toSeosuite */
    $toSeosuite = $modx->getObject(SeoSuiteResource::class, ['resource_id' => $to->get('id')]);
    if (!$fromSeosuite) {
        $modx->log(xPDO::LOG_LEVEL_ERROR, 'SeoSuiteResource from is not set', '', 'BabelTranslateResourceSeosuite3 Plugin');
        return;
    }
    if (!$toSeosuite) {
        $toSeosuite = $modx->newObject(SeoSuiteResource::class);
        $toSeosuite->set('resource_id', $to->get('id'));
    }
    if ($fromSeosuite->get('keywords')) {
        $keywords = array_map('trim', explode(',', $fromSeosuite->get('keywords')));
        foreach ($keywords as &$keyword) {
            $translation = $babeltranslate->translator->translate($keyword, $fromCulture, $toCulture);
            if ($translation) {
                $keyword = $translation;
            } else {
                if ($babeltranslate->getOption('debug')) {
                    $modx->log(xPDO::LOG_LEVEL_ERROR, 'Invalid translation result of SeoSuite keyword ' . $keyword, '', 'BabelTranslateResourceSeosuite Plugin');
                }
            }
        }
        $toSeosuite->set('keywords', implode(',', $keywords));
    } else {
        if ($babeltranslate->getOption('debug')) {
            $modx->log(xPDO::LOG_LEVEL_ERROR, 'The SeoSuite field keywords is empty', '', 'BabelTranslateResourceSeosuite Plugin');
        }
    }
    $toSeosuite->save();
}

return;

OnBabelTranslateCustomTV

The custom type of babeltranslate.translate_resource_tvs will be handled by a plugin in OnBabelTranslateCustomTV. The following code contains a plugin that translates the content of a MIGX template variable.

translatemigxtv.plugin.php
<?php
/**
 * BabelTranslateMigxTV Plugin
 *
 * @var modX $modx
 * @var array $scriptProperties
 */

use Sterc\SeoSuite\SeoSuite;
use Sterc\SeoSuite\Model\SeoSuiteResource;
use TreehillStudio\BabelTranslate\Translator\TranslatorInterface;

/** @var TranslatorInterface $translator */
$translator = $modx->getOption('translator', $scriptProperties);
/** @var modResource $resource */
$resource = $modx->getOption('resource', $scriptProperties);
/** @var string $tvname */
$tvname = $modx->getOption('tvname', $scriptProperties);
/** @var string $value */
$value = $modx->getOption('value', $scriptProperties);
/** @var modResource $to */
$fromCulture = $modx->getOption('fromCulture', $scriptProperties);
/** @var string $toCulture */
$toCulture = $modx->getOption('toCulture', $scriptProperties);

$corePath = $modx->getOption('babeltranslate.core_path', null, $modx->getOption('core_path') . 'components/babeltranslate/');
/** @var BabelTranslate $babeltranslate */
$babeltranslate = $modx->getService('babeltranslate', 'BabelTranslate', $corePath . 'model/babeltranslate/', [
    'core_path' => $corePath
]);

switch ($tvname) {
    case 'myTV': // Restrict the translation to the TV with this name.
        $migxFields = ['title', 'text']; // Translate the array values of MIGX TV using the keys in the array.
        break;
    default:
        $migxFields = [];
        break;
}

$migxValues = json_decode($value, true);
if (is_array($migxValues)) {
    foreach ($migxValues as &$migxValue) {
        foreach ($migxFields as $migxField) {
            $translation = $babeltranslate->translator->translate($migxValue[$migxField], $fromCulture, $toCulture);
            if ($translation) {
                $migxValue[$migxField] = $translation;
            } else {
                if ($babeltranslate->getOption('debug')) {
                    $modx->log(xPDO::LOG_LEVEL_ERROR, 'Invalid translation result of the value ' . $migxValue[$migxField] . '  of the MIGX TV ' . $tvname, '', 'BabelTranslateMigxTV Plugin');
                }
            }
        }
    }
}
$resource->setTVValue($tvname, json_encode($migxValues));

return;