You are here

public function LingotekConfigTranslationController::download in Lingotek Translation 3.3.x

Same name and namespace in other branches
  1. 8 src/Controller/LingotekConfigTranslationController.php \Drupal\lingotek\Controller\LingotekConfigTranslationController::download()
  2. 8.2 src/Controller/LingotekConfigTranslationController.php \Drupal\lingotek\Controller\LingotekConfigTranslationController::download()
  3. 4.0.x src/Controller/LingotekConfigTranslationController.php \Drupal\lingotek\Controller\LingotekConfigTranslationController::download()
  4. 3.0.x src/Controller/LingotekConfigTranslationController.php \Drupal\lingotek\Controller\LingotekConfigTranslationController::download()
  5. 3.1.x src/Controller/LingotekConfigTranslationController.php \Drupal\lingotek\Controller\LingotekConfigTranslationController::download()
  6. 3.2.x src/Controller/LingotekConfigTranslationController.php \Drupal\lingotek\Controller\LingotekConfigTranslationController::download()
  7. 3.4.x src/Controller/LingotekConfigTranslationController.php \Drupal\lingotek\Controller\LingotekConfigTranslationController::download()
  8. 3.5.x src/Controller/LingotekConfigTranslationController.php \Drupal\lingotek\Controller\LingotekConfigTranslationController::download()
  9. 3.6.x src/Controller/LingotekConfigTranslationController.php \Drupal\lingotek\Controller\LingotekConfigTranslationController::download()
  10. 3.7.x src/Controller/LingotekConfigTranslationController.php \Drupal\lingotek\Controller\LingotekConfigTranslationController::download()
  11. 3.8.x src/Controller/LingotekConfigTranslationController.php \Drupal\lingotek\Controller\LingotekConfigTranslationController::download()
1 string reference to 'LingotekConfigTranslationController::download'
lingotek.routing.yml in ./lingotek.routing.yml
lingotek.routing.yml

File

src/Controller/LingotekConfigTranslationController.php, line 502

Class

LingotekConfigTranslationController

Namespace

Drupal\lingotek\Controller

Code

public function download($entity_type, $entity_id, $locale, Request $request) {
  $drupal_language = $this->languageLocaleMapper
    ->getConfigurableLanguageForLocale($locale);
  $langcode = $drupal_language
    ->id();
  if ($entity_type === $entity_id) {

    // It is not a config entity, but a config object.
    $definition = $this->configMapperManager
      ->getDefinition($entity_type);
    try {
      $success = $this->translationService
        ->downloadConfig($entity_id, $locale);
      if ($success === TRUE) {
        $this
          ->messenger()
          ->addStatus($this
          ->t('Translation to %locale downloaded successfully', [
          '%locale' => $locale,
        ]));
      }
      elseif ($success === FALSE) {
        $mappers = $this->configMapperManager
          ->getMappers();
        $this->translationService
          ->setConfigTargetStatus($mappers[$entity_type], $langcode, Lingotek::STATUS_ERROR);
        $this
          ->messenger()
          ->addError($this
          ->t('%label @locale translation download failed. Please try again.', [
          '%label' => $definition['title'],
          '@locale' => $locale,
        ]));
      }
    } catch (LingotekApiException $e) {
      $mappers = $this->configMapperManager
        ->getMappers();
      $this->translationService
        ->setConfigTargetStatus($mappers[$entity_type], $langcode, Lingotek::STATUS_ERROR);
      $this
        ->messenger()
        ->addError($this
        ->t('%label @locale translation download failed. Please try again.', [
        '%label' => $definition['title'],
        '@locale' => $locale,
      ]));
    }
    return $this
      ->redirectToConfigTranslateOverview($entity_type);
  }

  // Check if it's a field.
  if (substr($entity_type, -7) == '_fields') {

    // Hack for fields, the entity is field config.
    $entity_type = 'field_config';
  }
  $entity = $this
    ->entityTypeManager()
    ->getStorage($entity_type)
    ->load($entity_id);
  try {
    $success = $this->translationService
      ->downloadDocument($entity, $locale);
    if ($success === TRUE) {
      $this
        ->messenger()
        ->addStatus($this
        ->t('Translation to %locale downloaded successfully', [
        '%locale' => $locale,
      ]));
    }
    elseif ($success === FALSE) {
      $this->translationService
        ->setTargetStatus($entity, $langcode, Lingotek::STATUS_ERROR);
      $this
        ->messenger()
        ->addError($this
        ->t('%label @locale translation download failed. Please try again.', [
        '%label' => $entity
          ->label(),
        '@locale' => $locale,
      ]));
    }
  } catch (LingotekApiException $e) {
    $this->translationService
      ->setTargetStatus($entity, $langcode, Lingotek::STATUS_ERROR);
    $this
      ->messenger()
      ->addError($this
      ->t('%label @locale translation download failed. Please try again.', [
      '%label' => $entity
        ->label(),
      '@locale' => $locale,
    ]));
  }
  return $this
    ->redirectToEntityTranslateOverview($entity_type, $entity_id);
}