3 namespace Drupal\locale\Form;
5 use Drupal\Core\Form\ConfigFormBase;
6 use Drupal\Core\Form\FormStateInterface;
9 * Configure locale settings for this site.
11 class LocaleSettingsForm extends ConfigFormBase {
16 public function getFormId() {
17 return 'locale_translate_settings';
23 protected function getEditableConfigNames() {
24 return ['locale.settings'];
30 public function buildForm(array $form, FormStateInterface $form_state) {
31 $config = $this->config('locale.settings');
33 $form['update_interval_days'] = [
35 '#title' => $this->t('Check for updates'),
36 '#default_value' => $config->get('translation.update_interval_days'),
38 '0' => $this->t('Never (manually)'),
39 '7' => $this->t('Weekly'),
40 '30' => $this->t('Monthly'),
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')]),
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')]);
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')]);
51 $form['#translation_directory'] = $directory;
52 $form['use_source'] = [
54 '#title' => $this->t('Translation source'),
55 '#default_value' => $config->get('translation.use_source'),
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'),
60 '#description' => $this->t('The source of translation files for automatic interface translation.') . ' ' . $description,
63 if ($config->get('translation.overwrite_not_customized') == FALSE) {
64 $default = LOCALE_TRANSLATION_OVERWRITE_NONE;
66 elseif ($config->get('translation.overwrite_customized') == TRUE) {
67 $default = LOCALE_TRANSLATION_OVERWRITE_ALL;
70 $default = LOCALE_TRANSLATION_OVERWRITE_NON_CUSTOMIZED;
72 $form['overwrite'] = [
74 '#title' => $this->t('Import behavior'),
75 '#default_value' => $default,
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.'),
81 '#description' => $this->t('How to treat existing translations when automatically updating the interface translations.'),
84 return parent::buildForm($form, $form_state);
90 public function validateForm(array &$form, FormStateInterface $form_state) {
91 parent::validateForm($form, $form_state);
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')]));
101 public function submitForm(array &$form, FormStateInterface $form_state) {
102 $values = $form_state->getValues();
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();
108 switch ($values['overwrite']) {
109 case LOCALE_TRANSLATION_OVERWRITE_ALL:
111 ->set('translation.overwrite_customized', TRUE)
112 ->set('translation.overwrite_not_customized', TRUE)
116 case LOCALE_TRANSLATION_OVERWRITE_NON_CUSTOMIZED:
118 ->set('translation.overwrite_customized', FALSE)
119 ->set('translation.overwrite_not_customized', TRUE)
123 case LOCALE_TRANSLATION_OVERWRITE_NONE:
125 ->set('translation.overwrite_customized', FALSE)
126 ->set('translation.overwrite_not_customized', FALSE)
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();
137 parent::submitForm($form, $form_state);