abstract class TranslatorPluginBase in Translation Management Tool 8
Default controller class for service plugins.
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\tmgmt\TranslatorPluginBase implements TranslatorPluginInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of TranslatorPluginBase
Related topics
3 files declare their use of TranslatorPluginBase
- FileTranslator.php in translators/
tmgmt_file/ src/ Plugin/ tmgmt/ Translator/ FileTranslator.php - LocalTranslator.php in translators/
tmgmt_local/ src/ Plugin/ tmgmt/ Translator/ LocalTranslator.php - TestTranslator.php in tmgmt_test/
src/ Plugin/ tmgmt/ Translator/ TestTranslator.php
File
- src/
TranslatorPluginBase.php, line 14
Namespace
Drupal\tmgmtView source
abstract class TranslatorPluginBase extends PluginBase implements TranslatorPluginInterface {
/**
* Characters that indicate the beginning of an escaped string.
*
* @var string
*/
protected $escapeStart = '';
/**
* Characters that indicate the end of an escaped string.
*
* @var string
*/
protected $escapeEnd = '';
/**
* {@inheritdoc}
*/
public function checkAvailable(TranslatorInterface $translator) {
// Assume that the translation service is always available.
return AvailableResult::yes();
}
/**
* {@inheritdoc}
*/
public function checkTranslatable(TranslatorInterface $translator, JobInterface $job) {
// The job is only translatable if the translator is available too.
if ($this
->checkAvailable($translator)
->getSuccess() && array_key_exists($job
->getTargetLangcode(), $translator
->getSupportedTargetLanguages($job
->getSourceLangcode()))) {
// We can only translate this job if the target language of the job is in
// one of the supported languages.
return TranslatableResult::yes();
}
return TranslatableResult::no(t('@translator can not translate from @source to @target.', array(
'@translator' => $translator
->label(),
'@source' => $job
->getSourceLanguage()
->getName(),
'@target' => $job
->getTargetLanguage()
->getName(),
)));
}
/**
* {@inheritdoc}
*/
public function abortTranslation(JobInterface $job) {
// Assume that we can abort a translation job at any time.
$job
->aborted();
return TRUE;
}
/**
* {@inheritdoc}
*/
public function getDefaultRemoteLanguagesMappings() {
return array();
}
/**
* {@inheritdoc}
*/
public function getSupportedRemoteLanguages(TranslatorInterface $translator) {
return array();
}
/**
* {@inheritdoc}
*/
public function getSupportedTargetLanguages(TranslatorInterface $translator, $source_language) {
$languages = tmgmt_available_languages();
return array_combine(array_keys($languages), array_keys($languages));
}
/**
* {@inheritdoc}
*
* Default implementation that gets target languages for each remote language.
* This approach is ineffective and therefore it is advised that a plugin
* should provide own implementation.
*/
public function getSupportedLanguagePairs(TranslatorInterface $translator) {
$language_pairs = array();
foreach ($this
->getSupportedRemoteLanguages($translator) as $source_language) {
foreach ($this
->getSupportedTargetLanguages($translator, $source_language) as $target_language) {
$language_pairs[] = array(
'source_language' => $source_language,
'target_language' => $target_language,
);
}
}
return $language_pairs;
}
/**
* {@inheritdoc}
*/
public function defaultSettings() {
$defaults = array();
// Check if any default settings are defined in the plugin info.
if (isset($this->pluginDefinition['default_settings'])) {
return array_merge($defaults, $this->pluginDefinition['default_settings']);
}
return $defaults;
}
/**
* {@inheritdoc}
*/
public function hasCheckoutSettings(JobInterface $job) {
return TRUE;
}
/**
* {@inheritdoc}
*/
public function acceptedDataItem(JobItemInterface $job_item, array $key) {
return TRUE;
}
/**
* {@inheritdoc}
*/
public function escapeText(array $data_item) {
if (empty($data_item['#escape'])) {
return $data_item['#text'];
}
$text = $data_item['#text'];
$escape = $data_item['#escape'];
// Sort them in reverse order based/ on the position and process them,
// so that positions don't change.
krsort($escape, SORT_NUMERIC);
foreach ($escape as $position => $info) {
$text = substr_replace($text, $this
->getEscapedString($info['string']), $position, strlen($info['string']));
}
return $text;
}
/**
* Returns the escaped string.
*
* Defaults to use the escapeStart and escapeEnd properties but can be
* overriden if a non-static replacement pattern is used.
*
* @param string $string
* String that should be escaped.
* @return string
*/
protected function getEscapedString($string) {
return $this->escapeStart . $string . $this->escapeEnd;
}
/**
* {@inheritdoc}
*/
public function unescapeText($text) {
return preg_replace('/' . preg_quote($this->escapeStart, '/') . '(.+)' . preg_quote($this->escapeEnd, '/') . '/U', '$1', $text);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginBase:: |
public | function | Constructs a \Drupal\Component\Plugin\PluginBase object. | 92 |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
TranslatorPluginBase:: |
protected | property | Characters that indicate the end of an escaped string. | 1 |
TranslatorPluginBase:: |
protected | property | Characters that indicate the beginning of an escaped string. | 1 |
TranslatorPluginBase:: |
public | function |
Aborts a translation job. Overrides TranslatorPluginInterface:: |
|
TranslatorPluginBase:: |
public | function |
Accept a single data item. Overrides TranslatorPluginInterface:: |
|
TranslatorPluginBase:: |
public | function |
Checks whether a translator is available. Overrides TranslatorPluginInterface:: |
1 |
TranslatorPluginBase:: |
public | function |
Check whether this service can handle a particular translation job. Overrides TranslatorPluginInterface:: |
2 |
TranslatorPluginBase:: |
public | function |
Defines default settings. Overrides TranslatorPluginInterface:: |
1 |
TranslatorPluginBase:: |
public | function |
Returns the escaped #text of a data item. Overrides TranslatorPluginInterface:: |
|
TranslatorPluginBase:: |
public | function |
Specifies default mappings for local to remote language codes. Overrides TranslatorPluginInterface:: |
1 |
TranslatorPluginBase:: |
protected | function | Returns the escaped string. | |
TranslatorPluginBase:: |
public | function |
Default implementation that gets target languages for each remote language.
This approach is ineffective and therefore it is advised that a plugin
should provide own implementation. Overrides TranslatorPluginInterface:: |
1 |
TranslatorPluginBase:: |
public | function |
Gets all supported languages of the translator. Overrides TranslatorPluginInterface:: |
|
TranslatorPluginBase:: |
public | function |
Returns all available target languages that are supported by this service
when given a source language. Overrides TranslatorPluginInterface:: |
2 |
TranslatorPluginBase:: |
public | function |
Returns if the translator has any settings for the passed job. Overrides TranslatorPluginInterface:: |
2 |
TranslatorPluginBase:: |
public | function |
Removes escape patterns from an escaped text. Overrides TranslatorPluginInterface:: |
|
TranslatorPluginInterface:: |
public | function | @abstract | 3 |