LocaleSettingsForm.php 5.09 KB
Newer Older
1 2 3 4 5 6 7 8
<?php
/**
 * @file
 * Contains \Drupal\user\LocaleSettingsForm.
 */

namespace Drupal\locale\Form;

9
use Drupal\Core\Form\ConfigFormBase;
10 11 12 13

/**
 * Configure locale settings for this site.
 */
14
class LocaleSettingsForm extends ConfigFormBase {
15 16

  /**
17
   * {@inheritdoc}
18
   */
19
  public function getFormId() {
20 21 22 23 24 25 26
    return 'locale_translate_settings';
  }

  /**
   * Implements \Drupal\Core\Form\FormInterface::buildForm().
   */
  public function buildForm(array $form, array &$form_state) {
27
    $config = $this->config('locale.settings');
28 29 30 31 32 33 34 35

    $form['update_interval_days'] = array(
      '#type' => 'radios',
      '#title' => t('Check for updates'),
      '#default_value' => $config->get('translation.update_interval_days'),
      '#options' => array(
        '0' => t('Never (manually)'),
        '7' => t('Weekly'),
36
        '30' => t('Monthly'),
37 38 39 40
      ),
      '#description' => t('Select how frequently you want to check for new interface translations for your currently installed modules and themes. <a href="@url">Check updates now</a>.', array('@url' => url('admin/reports/translations/check'))),
    );

41
    if ($directory = $config->get('translation.path')) {
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
      $description = t('Translation files are stored locally in the  %path directory. You can change this directory on the <a href="@url">File system</a> configuration page.', array('%path' => $directory, '@url' => url('admin/config/media/file-system')));
    }
    else {
      $description = t('Translation files will not be stored locally. Change the Interface translation directory on the <a href="@url">File system configuration</a> page.', array('@url' => url('admin/config/media/file-system')));
    }
    $form['#translation_directory'] = $directory;
    $form['use_source'] = array(
      '#type' => 'radios',
      '#title' => t('Translation source'),
      '#default_value' => $config->get('translation.use_source'),
      '#options' => array(
        LOCALE_TRANSLATION_USE_SOURCE_REMOTE_AND_LOCAL => t('Drupal translation server and local files'),
        LOCALE_TRANSLATION_USE_SOURCE_LOCAL => t('Local files only'),
      ),
      '#description' => t('The source of translation files for automatic interface translation.') . ' ' . $description,
    );

    if ($config->get('translation.overwrite_not_customized') == FALSE) {
      $default = LOCALE_TRANSLATION_OVERWRITE_NONE;
    }
    elseif ($config->get('translation.overwrite_customized') == TRUE) {
      $default = LOCALE_TRANSLATION_OVERWRITE_ALL;
    }
    else {
      $default = LOCALE_TRANSLATION_OVERWRITE_NON_CUSTOMIZED;
    }
    $form['overwrite'] = array(
      '#type' => 'radios',
70
      '#title' => t('Import behavior'),
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
      '#default_value' => $default,
      '#options' => array(
        LOCALE_TRANSLATION_OVERWRITE_NONE => t("Don't overwrite existing translations."),
        LOCALE_TRANSLATION_OVERWRITE_NON_CUSTOMIZED => t('Only overwrite imported translations, customized translations are kept.'),
        LOCALE_TRANSLATION_OVERWRITE_ALL => t('Overwrite existing translations.'),
      ),
      '#description' => t('How to treat existing translations when automatically updating the interface translations.'),
    );

    return parent::buildForm($form, $form_state);
  }

  /**
   * Implements \Drupal\Core\Form\FormInterface::validateForm().
   */
  public function validateForm(array &$form, array &$form_state) {
    parent::validateForm($form, $form_state);

    if (empty($form['#translation_directory']) && $form_state['values']['use_source'] == LOCALE_TRANSLATION_USE_SOURCE_LOCAL) {
90
      $this->setFormError('use_source', $form_state, $this->t('You have selected local translation source, but no <a href="@url">Interface translation directory</a> was configured.', array('@url' => url('admin/config/media/file-system'))));
91 92 93 94 95 96 97 98 99
    }
  }

  /**
   * Implements \Drupal\Core\Form\FormInterface::submitForm().
   */
  public function submitForm(array &$form, array &$form_state) {
    $values = $form_state['values'];

100
    $config = $this->config('locale.settings');
101 102 103 104 105 106 107
    $config->set('translation.update_interval_days', $values['update_interval_days'])->save();
    $config->set('translation.use_source', $values['use_source'])->save();

    switch ($values['overwrite']) {
      case LOCALE_TRANSLATION_OVERWRITE_ALL:
        $config
          ->set('translation.overwrite_customized', TRUE)
108 109
          ->set('translation.overwrite_not_customized', TRUE)
          ->save();
110
        break;
111

112 113 114
      case LOCALE_TRANSLATION_OVERWRITE_NON_CUSTOMIZED:
        $config
          ->set('translation.overwrite_customized', FALSE)
115 116
          ->set('translation.overwrite_not_customized', TRUE)
          ->save();
117
        break;
118

119 120 121
      case LOCALE_TRANSLATION_OVERWRITE_NONE:
        $config
          ->set('translation.overwrite_customized', FALSE)
122 123
          ->set('translation.overwrite_not_customized', FALSE)
          ->save();
124 125 126
        break;
    }

127 128
    // Invalidate the cached translation status when the configuration setting
    // of 'use_source' changes.
129
    if ($form['use_source']['#default_value'] != $form_state['values']['use_source']) {
130 131 132 133 134 135 136
      locale_translation_clear_status();
    }

    parent::submitForm($form, $form_state);
  }

}