You are here

class LocalTranslator in Translation Management Tool 8

Drupal user provider.

Plugin annotation


@TranslatorPlugin(
  id = "local",
  label = @Translation("Drupal user"),
  description = @Translation("Allows local users to process translation jobs."),
  ui = "\Drupal\tmgmt_local\LocalTranslatorUi",
  default_settings = {},
  map_remote_languages = FALSE
)

Hierarchy

Expanded class hierarchy of LocalTranslator

File

translators/tmgmt_local/src/Plugin/tmgmt/Translator/LocalTranslator.php, line 26

Namespace

Drupal\tmgmt_local\Plugin\tmgmt\Translator
View source
class LocalTranslator extends TranslatorPluginBase implements ContinuousTranslatorInterface {
  protected $language_pairs = array();

  /**
   * {@inheritdoc}
   */
  public function requestTranslation(JobInterface $job) {
    $items = $job
      ->getItems();
    $this
      ->requestJobItemsTranslation($items);

    // The translation job has been successfully submitted.
    $job
      ->submitted();
  }

  /**
   * {@inheritdoc}
   */
  public function requestJobItemsTranslation(array $job_items) {

    /** @var \Drupal\tmgmt\Entity\Job $job */
    $job = reset($job_items)
      ->getJob();
    $tuid = $job
      ->getSetting('translator');

    // Create local task for this job.

    /** @var \Drupal\tmgmt_local\LocalTaskInterface $local_task */
    $local_task = LocalTask::create(array(
      'uid' => $job
        ->getOwnerId(),
      'tuid' => $tuid,
      'tjid' => $job
        ->id(),
      'title' => $job
        ->label(),
    ));

    // If we have translator then switch to pending state.
    if ($tuid) {
      $local_task->status = LocalTaskInterface::STATUS_PENDING;
    }
    $local_task
      ->save();

    // Create task items.
    foreach ($job_items as $item) {
      $local_task
        ->addTaskItem($item);
      $item
        ->active();
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getSupportedTargetLanguages(TranslatorInterface $translator, $source_language) {
    $languages = tmgmt_local_supported_target_languages($source_language);
    if (\Drupal::config('tmgmt_local.settings')
      ->get('allow_all')) {
      $languages += parent::getSupportedTargetLanguages($translator, $source_language);
    }
    return $languages;
  }

  /**
   * {@inheritdoc}
   */
  public function getSupportedLanguagePairs(TranslatorInterface $translator) {
    if (!empty($this->language_pairs)) {
      return $this->language_pairs;
    }
    $roles = user_roles(TRUE, 'provide translation services');
    $query = \Drupal::database()
      ->select('field_data_tmgmt_translation_skills', 'ts');
    $query
      ->join('users', 'u', 'u.uid = ts.entity_id AND u.status = 1');
    $query
      ->addField('ts', 'tmgmt_translation_skills_language_from', 'source_language');
    $query
      ->addField('ts', 'tmgmt_translation_skills_language_to', 'target_language');
    $query
      ->condition('ts.deleted', 0);
    $query
      ->condition('ts.entity_type', 'user');
    if (!in_array(AccountInterface::AUTHENTICATED_ROLE, array_keys($roles))) {
      $query
        ->join('users_roles', 'ur', 'ur.uid = u.uid AND ur.rid');
      $or_conditions = (new Condition('OR'))
        ->condition('ur.rid', array_keys($roles), 'IN')
        ->condition('u.uid', 1);
      $query
        ->condition($or_conditions);
    }
    foreach ($query
      ->execute()
      ->fetchAll() as $item) {
      $this->language_pairs[] = array(
        'source_language' => $item->source_language,
        'target_language' => $item->target_language,
      );
    }
    return $this->language_pairs;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
LocalTranslator::$language_pairs protected property
LocalTranslator::getSupportedLanguagePairs 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 TranslatorPluginBase::getSupportedLanguagePairs
LocalTranslator::getSupportedTargetLanguages public function Returns all available target languages that are supported by this service when given a source language. Overrides TranslatorPluginBase::getSupportedTargetLanguages
LocalTranslator::requestJobItemsTranslation public function Requests the translation of a JobItem. Overrides ContinuousTranslatorInterface::requestJobItemsTranslation
LocalTranslator::requestTranslation public function @abstract Overrides TranslatorPluginInterface::requestTranslation
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 92
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
TranslatorPluginBase::$escapeEnd protected property Characters that indicate the end of an escaped string. 1
TranslatorPluginBase::$escapeStart protected property Characters that indicate the beginning of an escaped string. 1
TranslatorPluginBase::abortTranslation public function Aborts a translation job. Overrides TranslatorPluginInterface::abortTranslation
TranslatorPluginBase::acceptedDataItem public function Accept a single data item. Overrides TranslatorPluginInterface::acceptedDataItem
TranslatorPluginBase::checkAvailable public function Checks whether a translator is available. Overrides TranslatorPluginInterface::checkAvailable 1
TranslatorPluginBase::checkTranslatable public function Check whether this service can handle a particular translation job. Overrides TranslatorPluginInterface::checkTranslatable 2
TranslatorPluginBase::defaultSettings public function Defines default settings. Overrides TranslatorPluginInterface::defaultSettings 1
TranslatorPluginBase::escapeText public function Returns the escaped #text of a data item. Overrides TranslatorPluginInterface::escapeText
TranslatorPluginBase::getDefaultRemoteLanguagesMappings public function Specifies default mappings for local to remote language codes. Overrides TranslatorPluginInterface::getDefaultRemoteLanguagesMappings 1
TranslatorPluginBase::getEscapedString protected function Returns the escaped string.
TranslatorPluginBase::getSupportedRemoteLanguages public function Gets all supported languages of the translator. Overrides TranslatorPluginInterface::getSupportedRemoteLanguages
TranslatorPluginBase::hasCheckoutSettings public function Returns if the translator has any settings for the passed job. Overrides TranslatorPluginInterface::hasCheckoutSettings 2
TranslatorPluginBase::unescapeText public function Removes escape patterns from an escaped text. Overrides TranslatorPluginInterface::unescapeText