You are here

public function LingotekInterfaceTranslationService::requestTranslations in Lingotek Translation 3.3.x

Same name and namespace in other branches
  1. 4.0.x src/LingotekInterfaceTranslationService.php \Drupal\lingotek\LingotekInterfaceTranslationService::requestTranslations()
  2. 3.2.x src/LingotekInterfaceTranslationService.php \Drupal\lingotek\LingotekInterfaceTranslationService::requestTranslations()
  3. 3.4.x src/LingotekInterfaceTranslationService.php \Drupal\lingotek\LingotekInterfaceTranslationService::requestTranslations()
  4. 3.5.x src/LingotekInterfaceTranslationService.php \Drupal\lingotek\LingotekInterfaceTranslationService::requestTranslations()
  5. 3.6.x src/LingotekInterfaceTranslationService.php \Drupal\lingotek\LingotekInterfaceTranslationService::requestTranslations()
  6. 3.7.x src/LingotekInterfaceTranslationService.php \Drupal\lingotek\LingotekInterfaceTranslationService::requestTranslations()
  7. 3.8.x src/LingotekInterfaceTranslationService.php \Drupal\lingotek\LingotekInterfaceTranslationService::requestTranslations()

Requests translations of a document in all the enabled locales.

Parameters

string $component: The component being requested for translations.

Throws

\Drupal\lingotek\Exception\LingotekPaymentRequiredException

\Drupal\lingotek\Exception\LingotekDocumentArchivedException

\Drupal\lingotek\Exception\LingotekDocumentLockedException

\Drupal\lingotek\Exception\LingotekApiException

Overrides LingotekInterfaceTranslationServiceInterface::requestTranslations

File

src/LingotekInterfaceTranslationService.php, line 507

Class

LingotekInterfaceTranslationService
Service for managing Lingotek interface translations.

Namespace

Drupal\lingotek

Code

public function requestTranslations($component) {
  $languages = [];
  if ($document_id = $this
    ->getDocumentId($component)) {
    $target_languages = $this->languageManager
      ->getLanguages();
    $target_languages = array_filter($target_languages, function (LanguageInterface $language) {
      $configLanguage = ConfigurableLanguage::load($language
        ->getId());
      return $this->lingotekConfiguration
        ->isLanguageEnabled($configLanguage);
    });
    $source_langcode = 'en';
    foreach ($target_languages as $langcode => $language) {
      $locale = $this->languageLocaleMapper
        ->getLocaleForLangcode($langcode);
      if ($langcode !== $source_langcode) {
        $source_status = $this
          ->getSourceStatus($component);
        $current_status = $this
          ->getTargetStatus($component, $langcode);
        if ($current_status !== Lingotek::STATUS_PENDING && $current_status !== Lingotek::STATUS_CURRENT && $current_status !== Lingotek::STATUS_EDITED && $current_status !== Lingotek::STATUS_READY) {
          try {
            $result = $this->lingotek
              ->addTarget($document_id, $locale, NULL);
          } catch (LingotekDocumentLockedException $exception) {
            $this
              ->setDocumentId($component, $exception
              ->getNewDocumentId());
            throw $exception;
          } catch (LingotekDocumentArchivedException $exception) {
            $this
              ->setDocumentId($component, NULL);
            $this
              ->deleteMetadata($component);
            throw $exception;
          } catch (LingotekPaymentRequiredException $exception) {
            throw $exception;
          } catch (LingotekApiException $exception) {
            throw $exception;
          }
          if ($result) {
            $languages[] = $langcode;
            $this
              ->setTargetStatus($component, $langcode, Lingotek::STATUS_PENDING);

            // If the status was "Importing", and the target was added
            // successfully, we can ensure that the content is current now.
            if ($source_status == Lingotek::STATUS_IMPORTING) {
              $this
                ->setSourceStatus($component, Lingotek::STATUS_CURRENT);
            }
          }
        }
      }
    }
  }
  return $languages;
}