Version 1
[yaffs-website] / web / core / modules / locale / src / Form / LocaleSettingsForm.php
1 <?php
2
3 namespace Drupal\locale\Form;
4
5 use Drupal\Core\Form\ConfigFormBase;
6 use Drupal\Core\Form\FormStateInterface;
7
8 /**
9  * Configure locale settings for this site.
10  */
11 class LocaleSettingsForm extends ConfigFormBase {
12
13   /**
14    * {@inheritdoc}
15    */
16   public function getFormId() {
17     return 'locale_translate_settings';
18   }
19
20   /**
21    * {@inheritdoc}
22    */
23   protected function getEditableConfigNames() {
24     return ['locale.settings'];
25   }
26
27   /**
28    * {@inheritdoc}
29    */
30   public function buildForm(array $form, FormStateInterface $form_state) {
31     $config = $this->config('locale.settings');
32
33     $form['update_interval_days'] = [
34       '#type' => 'radios',
35       '#title' => $this->t('Check for updates'),
36       '#default_value' => $config->get('translation.update_interval_days'),
37       '#options' => [
38         '0' => $this->t('Never (manually)'),
39         '7' => $this->t('Weekly'),
40         '30' => $this->t('Monthly'),
41       ],
42       '#description' => $this->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>.', [':url' => $this->url('locale.check_translation')]),
43     ];
44
45     if ($directory = $config->get('translation.path')) {
46       $description = $this->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.', ['%path' => $directory, ':url' => $this->url('system.file_system_settings')]);
47     }
48     else {
49       $description = $this->t('Translation files will not be stored locally. Change the Interface translation directory on the <a href=":url">File system configuration</a> page.', [':url' => $this->url('system.file_system_settings')]);
50     }
51     $form['#translation_directory'] = $directory;
52     $form['use_source'] = [
53       '#type' => 'radios',
54       '#title' => $this->t('Translation source'),
55       '#default_value' => $config->get('translation.use_source'),
56       '#options' => [
57         LOCALE_TRANSLATION_USE_SOURCE_REMOTE_AND_LOCAL => $this->t('Drupal translation server and local files'),
58         LOCALE_TRANSLATION_USE_SOURCE_LOCAL => $this->t('Local files only'),
59       ],
60       '#description' => $this->t('The source of translation files for automatic interface translation.') . ' ' . $description,
61     ];
62
63     if ($config->get('translation.overwrite_not_customized') == FALSE) {
64       $default = LOCALE_TRANSLATION_OVERWRITE_NONE;
65     }
66     elseif ($config->get('translation.overwrite_customized') == TRUE) {
67       $default = LOCALE_TRANSLATION_OVERWRITE_ALL;
68     }
69     else {
70       $default = LOCALE_TRANSLATION_OVERWRITE_NON_CUSTOMIZED;
71     }
72     $form['overwrite'] = [
73       '#type' => 'radios',
74       '#title' => $this->t('Import behavior'),
75       '#default_value' => $default,
76       '#options' => [
77         LOCALE_TRANSLATION_OVERWRITE_NONE => $this->t("Don't overwrite existing translations."),
78         LOCALE_TRANSLATION_OVERWRITE_NON_CUSTOMIZED => $this->t('Only overwrite imported translations, customized translations are kept.'),
79         LOCALE_TRANSLATION_OVERWRITE_ALL => $this->t('Overwrite existing translations.'),
80       ],
81       '#description' => $this->t('How to treat existing translations when automatically updating the interface translations.'),
82     ];
83
84     return parent::buildForm($form, $form_state);
85   }
86
87   /**
88    * {@inheritdoc}
89    */
90   public function validateForm(array &$form, FormStateInterface $form_state) {
91     parent::validateForm($form, $form_state);
92
93     if (empty($form['#translation_directory']) && $form_state->getValue('use_source') == LOCALE_TRANSLATION_USE_SOURCE_LOCAL) {
94       $form_state->setErrorByName('use_source', $this->t('You have selected local translation source, but no <a href=":url">Interface translation directory</a> was configured.', [':url' => $this->url('system.file_system_settings')]));
95     }
96   }
97
98   /**
99    * {@inheritdoc}
100    */
101   public function submitForm(array &$form, FormStateInterface $form_state) {
102     $values = $form_state->getValues();
103
104     $config = $this->config('locale.settings');
105     $config->set('translation.update_interval_days', $values['update_interval_days'])->save();
106     $config->set('translation.use_source', $values['use_source'])->save();
107
108     switch ($values['overwrite']) {
109       case LOCALE_TRANSLATION_OVERWRITE_ALL:
110         $config
111           ->set('translation.overwrite_customized', TRUE)
112           ->set('translation.overwrite_not_customized', TRUE)
113           ->save();
114         break;
115
116       case LOCALE_TRANSLATION_OVERWRITE_NON_CUSTOMIZED:
117         $config
118           ->set('translation.overwrite_customized', FALSE)
119           ->set('translation.overwrite_not_customized', TRUE)
120           ->save();
121         break;
122
123       case LOCALE_TRANSLATION_OVERWRITE_NONE:
124         $config
125           ->set('translation.overwrite_customized', FALSE)
126           ->set('translation.overwrite_not_customized', FALSE)
127           ->save();
128         break;
129     }
130
131     // Invalidate the cached translation status when the configuration setting
132     // of 'use_source' changes.
133     if ($form['use_source']['#default_value'] != $form_state->getValue('use_source')) {
134       locale_translation_clear_status();
135     }
136
137     parent::submitForm($form, $form_state);
138   }
139
140 }