You are here

public function LingotekCliService::requestTranslations in Lingotek Translation 3.1.x

Same name and namespace in other branches
  1. 8.2 src/Cli/LingotekCliService.php \Drupal\lingotek\Cli\LingotekCliService::requestTranslations()
  2. 4.0.x src/Cli/LingotekCliService.php \Drupal\lingotek\Cli\LingotekCliService::requestTranslations()
  3. 3.0.x src/Cli/LingotekCliService.php \Drupal\lingotek\Cli\LingotekCliService::requestTranslations()
  4. 3.2.x src/Cli/LingotekCliService.php \Drupal\lingotek\Cli\LingotekCliService::requestTranslations()
  5. 3.3.x src/Cli/LingotekCliService.php \Drupal\lingotek\Cli\LingotekCliService::requestTranslations()
  6. 3.4.x src/Cli/LingotekCliService.php \Drupal\lingotek\Cli\LingotekCliService::requestTranslations()
  7. 3.5.x src/Cli/LingotekCliService.php \Drupal\lingotek\Cli\LingotekCliService::requestTranslations()
  8. 3.6.x src/Cli/LingotekCliService.php \Drupal\lingotek\Cli\LingotekCliService::requestTranslations()
  9. 3.7.x src/Cli/LingotekCliService.php \Drupal\lingotek\Cli\LingotekCliService::requestTranslations()
  10. 3.8.x src/Cli/LingotekCliService.php \Drupal\lingotek\Cli\LingotekCliService::requestTranslations()

File

src/Cli/LingotekCliService.php, line 100

Class

LingotekCliService

Namespace

Drupal\lingotek\Cli

Code

public function requestTranslations($entity_type_id, $entity_id, $langcodes = [
  'all',
]) {
  $entity = $this
    ->getEntity($entity_type_id, $entity_id);
  if ($entity instanceof EntityInterface) {
    $result = [];
    $languages = [];
    if (in_array('all', $langcodes)) {
      $languages = $this->translationService
        ->requestTranslations($entity);
    }
    else {
      foreach ($langcodes as $langcode) {
        $locale = $this->languageLocaleMapper
          ->getLocaleForLangcode($langcode);
        if ($locale) {
          $targetAdded = $this->translationService
            ->addTarget($entity, $locale);
          if ($targetAdded) {
            $languages[] = $langcode;
          }
          else {
            $this->logger
              ->error($this
              ->t('Language %langcode could not be requested.', [
              '%langcode' => $langcode,
            ]));
          }
        }
        else {
          $this->logger
            ->error($this
            ->t('Language %langcode is not valid.', [
            '%langcode' => $langcode,
          ]));
        }
      }
    }
    foreach ($languages as $langcode) {
      $result[$langcode] = [
        'langcode' => $langcode,
      ];
    }
    return $result;
  }

  // Contains an error message.
  return $entity;
}