For Developers

You can use Dmitryd\DdDeepl\Service\DeeplTranslationService class to translate TYPO3 records (must have an entry in $GLOBALS['TCA']), certain fields, or just texts. There is a number of events that can alter the behavior of the service. You can use them to get notified about translations, force or prevent a field from being translated or alter the field value before and after it is sent to DeepL.

API

Translation service

class DeeplTranslationService
Fully qualified name
\Dmitryd\DdDeepl\Service\DeeplTranslationService

This is the class you, as a developer, would use to translate data using DeepL.

__construct ( array $deeplOptions = [])

Creates the instance of the class. You can pass additional options as described in the DeepL documentation.

isAvailable ( ) : bool
returntype

bool

Returns

true if DeepL can process request with the current configuration and API limits.

translateRecord ( string $tableName, array $record, SiteLanguage $targetLanguage, array $exceptFieldNames = []) : array
returntype

array

The method will go through each field in the record, evaluate if it can be translated and call DeepL for translation. The result is an array with translations.

Returns

Array with translated fields

translateField ( string $tableName, string $fieldName, string $fieldValue, SiteLanguage $sourceLanguage, SiteLanguage $targetLanguage) : string
returntype

string

The method will get the value of the field and and call DeepL for translation. Unlike in translateRecord() there are no any kind of checks if the field can be translated at all.

Returns

Translated field value

translateText ( string $text, string $sourceLanguage, string $targetLanguage) : string
returntype

string

The method will get the value of the field and and call DeepL for translation. Unlike in translateRecord() there are no any kind of checks if the field can be translated at all.

Returns

Translated field value

Events

class AfterFieldTranslatedEvent
Fully qualified name
\Dmitryd\DdDeepl\Event\AfterFieldTranslatedEvent

This event is fired after the field was translated by translateRecord or translateField and allows to modify the translated value.

getTableName ( ) : string
returntype

string

Returns

Table name of the field

getFieldName ( ) : string
returntype

string

Returns

The field name

getFieldValue ( ) : string
returntype

string

Returns

The current (translated) field value

getSourceLanguage ( ) : SiteLanguage
returntype

\TYPO3\CMS\Core\Site\Entity\SiteLanguage

Returns

Source language to translate from

getTargetLanguage ( ) : SiteLanguage
returntype

\TYPO3\CMS\Core\Site\Entity\SiteLanguage

Returns

Target language to translate to

setFieldValue ( string $fieldValue) : void

Sets the new value of the field.

class AfterRecordTranslatedEvent
Fully qualified name
\Dmitryd\DdDeepl\Event\AfterRecordTranslatedEvent

This event is fired after the record was translated by translateRecord. You can examine fields and alter their contents by using getTranslatedFields and setTranslatedFields. Note that there is no method for getting the source language because you can get this information from the record.

getTableName ( ) : string
returntype

string

Returns

Table name of the field

getRecord ( ) : array
returntype

array

Returns

Original (non-translated) record

getTargetLanguage ( ) : SiteLanguage
returntype

\TYPO3\CMS\Core\Site\Entity\SiteLanguage

Returns

Target language to translate to

getTranslatedFields ( ) : array
returntype

array

Returns

The current (translated) field values

setTranslatedFields ( array $translatedFields) : void

Replaces translated fields with a new array of fields.

class BeforeFieldTranslationEvent
Fully qualified name
\Dmitryd\DdDeepl\Event\BeforeFieldTranslationEvent

This event is fired before the field is translated by translateRecord or translateField and allows to modify the original field value before it is sent to DeepL.

getTableName ( ) : string
returntype

string

Returns

Table name of the field

getFieldName ( ) : string
returntype

string

Returns

The field name

getFieldValue ( ) : string
returntype

string

Returns

The current field value

getSourceLanguage ( ) : SiteLanguage
returntype

\TYPO3\CMS\Core\Site\Entity\SiteLanguage

Returns

Source language to translate from

getTargetLanguage ( ) : SiteLanguage
returntype

\TYPO3\CMS\Core\Site\Entity\SiteLanguage

Returns

Target language to translate to

setFieldValue ( string $fieldValue) : void

Sets the new value of the field.

class BeforeRecordTranslationEvent
Fully qualified name
\Dmitryd\DdDeepl\Event\BeforeRecordTranslationEvent

This event is fired before the record is translated by translateRecord. You can examine fields and alter their contents by using getTranslatedFields and setTranslatedFields. Note that there is no method for getting the source language because you can get this information from the record.

getTableName ( ) : string
returntype

string

Returns

Table name of the field

getRecord ( ) : array
returntype

array

Returns

Original (non-translated) record

getTargetLanguage ( ) : SiteLanguage
returntype

\TYPO3\CMS\Core\Site\Entity\SiteLanguage

Returns

Target language to translate to

getTranslatedFields ( ) : array
returntype

array

Returns

The current field values

setTranslatedFields ( array $translatedFields) : void

Replaces translated fields with a new array of fields.

class CanFieldBeTranslatedCheckEvent
Fully qualified name
\Dmitryd\DdDeepl\Event\CanFieldBeTranslatedCheckEvent

This event is fired after the DeepL translation service evaluated whether the field can be translated.

getTableName ( ) : string
returntype

string

Returns

Table name of the field

getFieldName ( ) : string
returntype

string

Returns

The field name

getCanBeTranslated(): ?bool ( )
returntype

?bool

Returns

true, if the service thinks that the field can be translated, false, if definitely not, null, if the service could not decide

setCanBeTranslated ( ) : void

Pass true, if the service thinks that the field can be translated, false, if not. Note that you cannot pass null here. If you are unsure, do not set any value. The service will not translate the field unless the value after all events is set to true.

class PreprocessFieldValueEvent
Fully qualified name
\Dmitryd\DdDeepl\Event\PreprocessFieldValueEvent

This event is fired before the field is set to DeepL for translation and allows you to modify the value. A typical example would be, for example, doing data clean up or replacing &nbsp; with a normal space in texts, or removing several <br> tags.

getTableName ( ) : string
returntype

string

Returns

Table name of the field

getFieldName ( ) : string
returntype

string

Returns

The field name

getFieldValue ( ) : string
returntype

string

Returns

The current field value

setFieldValue ( string $fieldValue) : void

Sets the new value of the field.

Examples

Translating a record:

$languageId = 1;
$newsId = 1;
$newsRecord = BackendUtility::getRecord('tx_news_domain_model_news', $newsId);
$site = GeneralUtility::makeInstance(SiteFinder::class)->getSiteByPageId($newsRecord['pid']);
$targetLanguage = $site->getLanguageById($languageId);
$service = GeneralUtility::makeInstance(\Dmitryd\DdDeepl\Service\DeeplTranslationService::class);
$translatedFields = $service->translateRecord('tx_news_domain_model_news', $newsRecord, $targetLanguage);
Copied!