Commit 4b172c72 authored by alexpott's avatar alexpott

Issue #1978918 by likin, Luxian, vijaycs85, Pancho, disasm: Convert...

Issue #1978918 by likin, Luxian, vijaycs85, Pancho, disasm: Convert locale_translate_import_form to a Controller.
parent 4ad6fc90
<?php
/**
* @file
* Contains \Drupal\locale\Form\ImportForm.
*/
namespace Drupal\locale\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Language\Language;
use Drupal\Core\Language\LanguageManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Form constructor for the translation import screen.
*/
class ImportForm extends FormBase implements ContainerInjectionInterface {
/**
* Uploaded file entity.
*
* @var \Drupal\file\Entity\File
*/
protected $file;
/**
* The module handler service.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('module_handler'),
$container->get('language_manager')
);
}
/**
* Constructs a form for language import.
*
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler service.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
*/
public function __construct(ModuleHandlerInterface $module_handler, LanguageManagerInterface $language_manager) {
$this->moduleHandler = $module_handler;
$this->languageManager = $language_manager;
}
/**
* {@inheritdoc}
*/
public function getFormID() {
return 'locale_translate_import_form';
}
/**
* Form constructor for the translation import screen.
*
* @ingroup forms
*/
public function buildForm(array $form, array &$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->name;
}
}
// 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.
form_load_include($form_state, 'inc', 'language', 'language.admin');
if (empty($existing_languages)) {
$language_options = language_admin_predefined_list();
$default = key($language_options);
}
else {
$default = key($existing_languages);
$language_options = array(
$this->t('Existing languages') => $existing_languages,
$this->t('Languages not yet added') => language_admin_predefined_list()
);
}
$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 validateForm(array &$form, array &$form_state) {
$this->file = file_save_upload('file', $form['file']['#upload_validators'], 'translations://', 0);
// Ensure we have the file uploaded.
if (!$this->file) {
$this->setFormError('file', $form_state, $this->t('File to import not found.'));
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, array &$form_state) {
// Add language, if not yet supported.
$language = $this->languageManager->getLanguage($form_state['values']['langcode']);
if (empty($language)) {
$language = new Language(array(
'id' => $form_state['values']['langcode']
));
$language = language_save($language);
drupal_set_message($this->t('The language %language has been created.', array('%language' => $this->t($language->name))));
}
$options = array(
'langcode' => $form_state['values']['langcode'],
'overwrite_options' => $form_state['values']['overwrite_options'],
'customized' => $form_state['values']['customized'] ? LOCALE_CUSTOMIZED : LOCALE_NOT_CUSTOMIZED,
);
$this->moduleHandler->loadInclude('locale', 'bulk.inc');
$file = locale_translate_file_attach_properties($this->file, $options);
$batch = locale_translate_batch_build(array($file->uri => $file), $options);
batch_set($batch);
$form_state['redirect_route']['route_name'] = 'locale.translate_page';
}
}
......@@ -11,16 +11,6 @@
*/
class LocaleForm {
/**
* Wraps locale_translate_import_form().
*
* @todo Remove locale_translate_import_form().
*/
public function import() {
module_load_include('bulk.inc', 'locale');
return \Drupal::formBuilder()->getForm('locale_translate_import_form');
}
/**
* Wraps locale_translation_status_form().
*
......
......@@ -9,142 +9,6 @@
use Drupal\Core\Language\Language;
use Drupal\file\FileInterface;
/**
* Form constructor for the translation import screen.
*
* @see locale_translate_import_form_submit()
* @ingroup forms
*
* @deprecated in Drupal 8.x-dev, will be removed before Drupal 8.0.
* Use \Drupal\locale\Form\LocaleForm::import().
*/
function locale_translate_import_form($form, &$form_state) {
Drupal::languageManager()->reset();
$languages = \Drupal::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->name;
}
}
// 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.
form_load_include($form_state, 'inc', 'language', 'language.admin');
if (empty($existing_languages)) {
$language_options = language_admin_predefined_list();
$default = key($language_options);
}
else {
$default = key($existing_languages);
$language_options = array(
t('Existing languages') => $existing_languages,
t('Languages not yet added') => language_admin_predefined_list()
);
}
$validators = array(
'file_validate_extensions' => array('po'),
'file_validate_size' => array(file_upload_max_size()),
);
$file_description = array(
'#theme' => 'file_upload_help',
'#description' => t('A Gettext Portable Object file.'),
'#upload_validators' => $validators,
);
$form['file'] = array(
'#type' => 'file',
'#title' => t('Translation file'),
'#description' => drupal_render($file_description),
'#size' => 50,
'#upload_validators' => $validators,
'#attributes' => array('class' => array('file-import-input')),
'#attached' => array(
'js' => array(
drupal_get_path('module', 'locale') . '/locale.bulk.js' => array(),
),
),
);
$form['langcode'] = array(
'#type' => 'select',
'#title' => t('Language'),
'#options' => $language_options,
'#default_value' => $default,
'#attributes' => array('class' => array('langcode-input')),
);
$form['customized'] = array(
'#title' => t('Treat imported strings as custom translations'),
'#type' => 'checkbox',
);
$form['overwrite_options'] = array(
'#type' => 'container',
'#tree' => TRUE,
);
$form['overwrite_options']['not_customized'] = array(
'#title' => t('Overwrite non-customized translations'),
'#type' => 'checkbox',
'#states' => array(
'checked' => array(
':input[name="customized"]' => array('checked' => TRUE),
),
),
);
$form['overwrite_options']['customized'] = array(
'#title' => t('Overwrite existing customized translations'),
'#type' => 'checkbox',
);
$form['actions'] = array(
'#type' => 'actions'
);
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Import')
);
return $form;
}
/**
* Form submission handler for locale_translate_import_form().
*/
function locale_translate_import_form_submit($form, &$form_state) {
// Ensure we have the file uploaded.
if ($file = file_save_upload('file', $form['file']['#upload_validators'], 'translations://', 0)) {
// Add language, if not yet supported.
$language = \Drupal::languageManager()->getLanguage($form_state['values']['langcode']);
if (empty($language)) {
$language = new Language(array(
'id' => $form_state['values']['langcode']
));
$language = language_save($language);
drupal_set_message(t('The language %language has been created.', array('%language' => t($language->name))));
}
$options = array(
'langcode' => $form_state['values']['langcode'],
'overwrite_options' => $form_state['values']['overwrite_options'],
'customized' => $form_state['values']['customized'] ? LOCALE_CUSTOMIZED : LOCALE_NOT_CUSTOMIZED,
);
$file = locale_translate_file_attach_properties($file, $options);
$batch = locale_translate_batch_build(array($file->uri => $file), $options);
batch_set($batch);
}
else {
drupal_set_message(t('File to import not found.'), 'error');
$form_state['rebuild'] = TRUE;
return;
}
$form_state['redirect_route']['route_name'] = 'locale.translate_page';
return;
}
/**
* Prepare a batch to import all translations.
*
......
......@@ -24,7 +24,7 @@ locale.translate_page:
locale.translate_import:
path: '/admin/config/regional/translate/import'
defaults:
_content: '\Drupal\locale\Form\LocaleForm::import'
_form: '\Drupal\locale\Form\ImportForm'
_title: 'Import'
requirements:
_permission: 'translate interface'
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment