ExportForm.php 5.45 KB
Newer Older
1 2 3 4 5
<?php

namespace Drupal\locale\Form;

use Drupal\Component\Gettext\PoStreamWriter;
6
use Drupal\Core\File\FileSystemInterface;
7
use Drupal\Core\Form\FormBase;
8
use Drupal\Core\Form\FormStateInterface;
9
use Drupal\Core\Language\LanguageInterface;
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\locale\PoDatabaseReader;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\BinaryFileResponse;

/**
 * Form for the Gettext translation files export form.
 */
class ExportForm extends FormBase {

  /**
   * The language manager.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface
   */
  protected $languageManager;

27 28 29 30 31 32 33
  /**
   * The file system service.
   *
   * @var \Drupal\Core\File\FileSystemInterface
   */
  protected $fileSystem;

34 35 36 37 38
  /**
   * Constructs a new ExportForm.
   *
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   *   The language manager.
39 40
   * @param \Drupal\Core\File\FileSystemInterface $file_system
   *   The file system service.
41
   */
42
  public function __construct(LanguageManagerInterface $language_manager, FileSystemInterface $file_system) {
43
    $this->languageManager = $language_manager;
44
    $this->fileSystem = $file_system;
45 46 47 48 49 50 51
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
52 53
      $container->get('language_manager'),
      $container->get('file_system')
54 55 56 57 58 59
    );
  }

  /**
   * {@inheritdoc}
   */
60
  public function getFormId() {
61 62 63 64 65 66
    return 'locale_translate_export_form';
  }

  /**
   * {@inheritdoc}
   */
67
  public function buildForm(array $form, FormStateInterface $form_state) {
68 69 70
    $languages = $this->languageManager->getLanguages();
    $language_options = array();
    foreach ($languages as $langcode => $language) {
71
      if (locale_is_translatable($langcode)) {
72
        $language_options[$langcode] = $language->getName();
73 74 75 76 77 78 79
      }
    }
    $language_default = $this->languageManager->getDefaultLanguage();

    if (empty($language_options)) {
      $form['langcode'] = array(
        '#type' => 'value',
80
        '#value' => LanguageInterface::LANGCODE_SYSTEM,
81 82 83 84 85 86 87 88 89 90 91 92
      );
      $form['langcode_text'] = array(
        '#type' => 'item',
        '#title' => $this->t('Language'),
        '#markup' => $this->t('No language available. The export will only contain source strings.'),
      );
    }
    else {
      $form['langcode'] = array(
        '#type' => 'select',
        '#title' => $this->t('Language'),
        '#options' => $language_options,
93
        '#default_value' => $language_default->getId(),
94
        '#empty_option' => $this->t('Source text only, no translations'),
95
        '#empty_value' => LanguageInterface::LANGCODE_SYSTEM,
96 97 98 99 100 101 102
      );
      $form['content_options'] = array(
        '#type' => 'details',
        '#title' => $this->t('Export options'),
        '#tree' => TRUE,
        '#states' => array(
          'invisible' => array(
103
            ':input[name="langcode"]' => array('value' => LanguageInterface::LANGCODE_SYSTEM),
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
          ),
        ),
      );
      $form['content_options']['not_customized'] = array(
        '#type' => 'checkbox',
        '#title' => $this->t('Include non-customized translations'),
        '#default_value' => TRUE,
      );
      $form['content_options']['customized'] = array(
        '#type' => 'checkbox',
        '#title' => $this->t('Include customized translations'),
        '#default_value' => TRUE,
      );
      $form['content_options']['not_translated'] = array(
        '#type' => 'checkbox',
        '#title' => $this->t('Include untranslated text'),
        '#default_value' => TRUE,
      );
    }

    $form['actions'] = array(
125
      '#type' => 'actions',
126 127 128
    );
    $form['actions']['submit'] = array(
      '#type' => 'submit',
129
      '#value' => $this->t('Export'),
130 131 132 133 134 135 136
    );
    return $form;
  }

  /**
   * {@inheritdoc}
   */
137
  public function submitForm(array &$form, FormStateInterface $form_state) {
138
    // If template is required, language code is not given.
139 140
    if ($form_state->getValue('langcode') != LanguageInterface::LANGCODE_SYSTEM) {
      $language = $this->languageManager->getLanguage($form_state->getValue('langcode'));
141 142 143 144
    }
    else {
      $language = NULL;
    }
145
    $content_options = $form_state->getValue('content_options', array());
146
    $reader = new PoDatabaseReader();
147
    $language_name = '';
148
    if ($language != NULL) {
149
      $reader->setLangcode($language->getId());
150 151
      $reader->setOptions($content_options);
      $languages = $this->languageManager->getLanguages();
152
      $language_name = isset($languages[$language->getId()]) ? $languages[$language->getId()]->getName() : '';
153
      $filename = $language->getId() . '.po';
154 155 156 157 158 159 160 161
    }
    else {
      // Template required.
      $filename = 'drupal.pot';
    }

    $item = $reader->readItem();
    if (!empty($item)) {
162
      $uri = $this->fileSystem->tempnam('temporary://', 'po_');
163 164
      $header = $reader->getHeader();
      $header->setProjectName($this->config('system.site')->get('name'));
165
      $header->setLanguageName($language_name);
166

167
      $writer = new PoStreamWriter();
168 169 170 171 172 173 174 175 176 177
      $writer->setUri($uri);
      $writer->setHeader($header);

      $writer->open();
      $writer->writeItem($item);
      $writer->writeItems($reader);
      $writer->close();

      $response = new BinaryFileResponse($uri);
      $response->setContentDisposition('attachment', $filename);
178
      $form_state->setResponse($response);
179 180 181 182 183 184 185
    }
    else {
      drupal_set_message($this->t('Nothing to export.'));
    }
  }

}