/** * Form constructor for the translation import screen. */ public function buildForm(array $form, FormStateInterface $form_state) { $languages = $this->languageManager->getLanguages(); // Initialize a language list to the ones available, including English if we // are to translate Drupal to English as well. $existing_languages = array(); foreach ($languages as $langcode => $language) { if ($langcode != 'en' || locale_translate_english()) { $existing_languages[$langcode] = $language->getName(); } } // If we have no languages available, present the list of predefined // languages only. If we do have already added languages, set up two option // groups with the list of existing and then predefined languages. if (empty($existing_languages)) { $language_options = $this->languageManager->getStandardLanguageListWithoutConfigured(); $default = key($language_options); } else { $default = key($existing_languages); $language_options = array($this->t('Existing languages') => $existing_languages, $this->t('Languages not yet added') => $this->languageManager->getStandardLanguageListWithoutConfigured()); } $validators = array('file_validate_extensions' => array('po'), 'file_validate_size' => array(file_upload_max_size())); $form['file'] = array('#type' => 'file', '#title' => $this->t('Translation file'), '#description' => array('#theme' => 'file_upload_help', '#description' => $this->t('A Gettext Portable Object file.'), '#upload_validators' => $validators), '#size' => 50, '#upload_validators' => $validators, '#attributes' => array('class' => array('file-import-input'))); $form['langcode'] = array('#type' => 'select', '#title' => $this->t('Language'), '#options' => $language_options, '#default_value' => $default, '#attributes' => array('class' => array('langcode-input'))); $form['customized'] = array('#title' => $this->t('Treat imported strings as custom translations'), '#type' => 'checkbox'); $form['overwrite_options'] = array('#type' => 'container', '#tree' => TRUE); $form['overwrite_options']['not_customized'] = array('#title' => $this->t('Overwrite non-customized translations'), '#type' => 'checkbox', '#states' => array('checked' => array(':input[name="customized"]' => array('checked' => TRUE)))); $form['overwrite_options']['customized'] = array('#title' => $this->t('Overwrite existing customized translations'), '#type' => 'checkbox'); $form['actions'] = array('#type' => 'actions'); $form['actions']['submit'] = array('#type' => 'submit', '#value' => $this->t('Import')); return $form; }
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { $languages = $this->languageManager->getLanguages(); $language_options = array(); foreach ($languages as $langcode => $language) { if ($langcode != 'en' || locale_translate_english()) { $language_options[$langcode] = $language->getName(); } } $language_default = $this->languageManager->getDefaultLanguage(); if (empty($language_options)) { $form['langcode'] = array('#type' => 'value', '#value' => LanguageInterface::LANGCODE_SYSTEM); $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->getId(), '#empty_option' => $this->t('Source text only, no translations'), '#empty_value' => LanguageInterface::LANGCODE_SYSTEM); $form['content_options'] = array('#type' => 'details', '#title' => $this->t('Export options'), '#collapsed' => TRUE, '#tree' => TRUE, '#states' => array('invisible' => array(':input[name="langcode"]' => array('value' => LanguageInterface::LANGCODE_SYSTEM)))); $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('#type' => 'actions'); $form['actions']['submit'] = array('#type' => 'submit', '#value' => $this->t('Export')); return $form; }
/** * Lists locale translation filters that can be applied. */ protected function translateFilters() { $filters = array(); // Get all languages, except English. $this->languageManager->reset(); $languages = $this->languageManager->getLanguages(); $language_options = array(); foreach ($languages as $langcode => $language) { if ($langcode != 'en' || locale_translate_english()) { $language_options[$langcode] = $language->getName(); } } // Pick the current interface language code for the filter. $default_langcode = $this->languageManager->getCurrentLanguage()->getId(); if (!isset($language_options[$default_langcode])) { $available_langcodes = array_keys($language_options); $default_langcode = array_shift($available_langcodes); } $filters['string'] = array('title' => $this->t('String contains'), 'description' => $this->t('Leave blank to show all strings. The search is case sensitive.'), 'default' => ''); $filters['langcode'] = array('title' => $this->t('Translation language'), 'options' => $language_options, 'default' => $default_langcode); $filters['translation'] = array('title' => $this->t('Search in'), 'options' => array('all' => $this->t('Both translated and untranslated strings'), 'translated' => $this->t('Only translated strings'), 'untranslated' => $this->t('Only untranslated strings')), 'default' => 'all'); $filters['customized'] = array('title' => $this->t('Translation type'), 'options' => array('all' => $this->t('All'), LOCALE_NOT_CUSTOMIZED => $this->t('Non-customized translation'), LOCALE_CUSTOMIZED => $this->t('Customized translation')), 'states' => array('visible' => array(':input[name=translation]' => array('value' => 'translated'))), 'default' => 'all'); return $filters; }