ExportForm.php 5.13 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
<?php

/**
 * @file
 * Contains \Drupal\locale\Form\ExportForm.
 */

namespace Drupal\locale\Form;

use Drupal\Component\Gettext\PoStreamWriter;
use Drupal\Core\Form\FormBase;
12
use Drupal\Core\Language\LanguageInterface;
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
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;

  /**
   * Constructs a new ExportForm.
   *
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   *   The language manager.
   */
  public function __construct(LanguageManagerInterface $language_manager) {
    $this->languageManager = $language_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('language_manager')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function getFormID() {
    return 'locale_translate_export_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, array &$form_state) {
    $languages = $this->languageManager->getLanguages();
    $language_options = array();
    foreach ($languages as $langcode => $language) {
      if ($langcode != 'en' || locale_translate_english()) {
        $language_options[$langcode] = $language->name;
      }
    }
    $language_default = $this->languageManager->getDefaultLanguage();

    if (empty($language_options)) {
      $form['langcode'] = array(
        '#type' => 'value',
72
        '#value' => LanguageInterface::LANGCODE_SYSTEM,
73 74 75 76 77 78 79 80 81 82 83 84 85 86
      );
      $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,
        '#default_value' => $language_default->id,
        '#empty_option' => $this->t('Source text only, no translations'),
87
        '#empty_value' => LanguageInterface::LANGCODE_SYSTEM,
88 89 90 91 92 93 94 95
      );
      $form['content_options'] = array(
        '#type' => 'details',
        '#title' => $this->t('Export options'),
        '#collapsed' => TRUE,
        '#tree' => TRUE,
        '#states' => array(
          'invisible' => array(
96
            ':input[name="langcode"]' => array('value' => LanguageInterface::LANGCODE_SYSTEM),
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
          ),
        ),
      );
      $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(
118
      '#type' => 'actions',
119 120 121
    );
    $form['actions']['submit'] = array(
      '#type' => 'submit',
122
      '#value' => $this->t('Export'),
123 124 125 126 127 128 129 130 131
    );
    return $form;
  }

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

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

160
      $writer = new PoStreamWriter();
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
      $writer->setUri($uri);
      $writer->setHeader($header);

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

      $response = new BinaryFileResponse($uri);
      $response->setContentDisposition('attachment', $filename);
      $form_state['response'] = $response;
    }
    else {
      drupal_set_message($this->t('Nothing to export.'));
    }
  }

}