For Developers
You can use Dmitryd\
class to translate TYPO3 records (must have
an entry in $GLOBALS
), 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\
Dd Deepl\ Service\ Deepl Translation Service
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
translate
there are no any kind of checks if the field can be translated at all.Record () - 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
translate
there are no any kind of checks if the field can be translated at all.Record () - Returns
-
Translated field value
Events
- class AfterFieldTranslatedEvent
-
- Fully qualified name
-
\Dmitryd\
Dd Deepl\ Event\ After Field Translated Event
This event is fired after the field was translated by
translate
orRecord translate
and allows to modify the translated value.Field - getSourceLanguage ( ) : SiteLanguage
-
- returntype
-
\TYPO3\CMS\Core\Site\Entity\SiteLanguage
- Returns
-
Source language to translate from
- class AfterRecordTranslatedEvent
-
- Fully qualified name
-
\Dmitryd\
Dd Deepl\ Event\ After Record Translated Event
This event is fired after the record was translated by
translate
. You can examine fields and alter their contents by usingRecord get
andTranslated Fields set
. Note that there is no method for getting the source language because you can get this information from the record.Translated Fields
- class BeforeFieldTranslationEvent
-
- Fully qualified name
-
\Dmitryd\
Dd Deepl\ Event\ Before Field Translation Event
This event is fired before the field is translated by
translate
orRecord translate
and allows to modify the original field value before it is sent to DeepL.Field - getSourceLanguage ( ) : SiteLanguage
-
- returntype
-
\TYPO3\CMS\Core\Site\Entity\SiteLanguage
- Returns
-
Source language to translate from
- class BeforeRecordTranslationEvent
-
- Fully qualified name
-
\Dmitryd\
Dd Deepl\ Event\ Before Record Translation Event
This event is fired before the record is translated by
translate
. You can examine fields and alter their contents by usingRecord get
andTranslated Fields set
. Note that there is no method for getting the source language because you can get this information from the record.Translated Fields
- class CanFieldBeTranslatedCheckEvent
-
- Fully qualified name
-
\Dmitryd\
Dd Deepl\ Event\ Can Field Be Translated Check Event
This event is fired after the DeepL translation service evaluated whether the field can be translated.
- class PreprocessFieldValueEvent
-
- Fully qualified name
-
\Dmitryd\
Dd Deepl\ Event\ Preprocess Field Value Event
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
with a normal space in texts, or removing several<br>
tags.
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);