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 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
Return type

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
Return type

array

Returns

Array with translated fields

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.

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

string

Returns

Translated field value

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.

translateText(string $text, string $sourceLanguage, string $targetLanguage): string
Return type

string

Returns

Translated field value

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.

Events

class 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
Return type

string

Returns

Table name of the field

getFieldName(): string
Return type

string

Returns

The field name

getFieldValue(): string
Return type

string

Returns

The current (translated) field value

getSourceLanguage(): SiteLanguage
Return type

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

Returns

Source language to translate from

getTargetLanguage(): SiteLanguage
Return type

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

Returns

Target language to translate to

setFieldValue(string $fieldValue): void

Sets the new value of the field.

class 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
Return type

string

Returns

Table name of the field

getRecord(): array
Return type

array

Returns

Original (non-translated) record

getTargetLanguage(): SiteLanguage
Return type

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

Returns

Target language to translate to

getTranslatedFields(): array
Return type

array

Returns

The current (translated) field values

setTranslatedFields(array $translatedFields): void

Replaces translated fields with a new array of fields.

class 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
Return type

string

Returns

Table name of the field

getFieldName(): string
Return type

string

Returns

The field name

getFieldValue(): string
Return type

string

Returns

The current field value

getSourceLanguage(): SiteLanguage
Return type

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

Returns

Source language to translate from

getTargetLanguage(): SiteLanguage
Return type

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

Returns

Target language to translate to

setFieldValue(string $fieldValue): void

Sets the new value of the field.

class 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
Return type

string

Returns

Table name of the field

getRecord(): array
Return type

array

Returns

Original (non-translated) record

getTargetLanguage(): SiteLanguage
Return type

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

Returns

Target language to translate to

getTranslatedFields(): array
Return type

array

Returns

The current field values

setTranslatedFields(array $translatedFields): void

Replaces translated fields with a new array of fields.

class Dmitryd\DdDeepl\Event\CanFieldBeTranslatedCheckEvent

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

getTableName(): string
Return type

string

Returns

Table name of the field

getFieldName(): string
Return type

string

Returns

The field name

getCanBeTranslated(): ?bool
Return type

?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 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
Return type

string

Returns

Table name of the field

getFieldName(): string
Return type

string

Returns

The field name

getFieldValue(): string
Return type

string

Returns

The current field value

setFieldValue(string $fieldValue): void

Sets the new value of the field.

Examples

Translating a record:

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