3 namespace Drupal\locale\Form;
5 use Drupal\Core\Form\FormBase;
6 use Drupal\Core\Extension\ModuleHandlerInterface;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\language\ConfigurableLanguageManagerInterface;
9 use Drupal\language\Entity\ConfigurableLanguage;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
13 * Form constructor for the translation import screen.
17 class ImportForm extends FormBase {
20 * Uploaded file entity.
22 * @var \Drupal\file\Entity\File
27 * The module handler service.
29 * @var \Drupal\Core\Extension\ModuleHandlerInterface
31 protected $moduleHandler;
34 * The configurable language manager.
36 * @var \Drupal\language\ConfigurableLanguageManagerInterface
38 protected $languageManager;
43 public static function create(ContainerInterface $container) {
45 $container->get('module_handler'),
46 $container->get('language_manager')
50 * Constructs a form for language import.
52 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
53 * The module handler service.
54 * @param \Drupal\language\ConfigurableLanguageManagerInterface $language_manager
55 * The configurable language manager.
57 public function __construct(ModuleHandlerInterface $module_handler, ConfigurableLanguageManagerInterface $language_manager) {
58 $this->moduleHandler = $module_handler;
59 $this->languageManager = $language_manager;
65 public function getFormId() {
66 return 'locale_translate_import_form';
70 * Form constructor for the translation import screen.
72 public function buildForm(array $form, FormStateInterface $form_state) {
73 $languages = $this->languageManager->getLanguages();
75 // Initialize a language list to the ones available, including English if we
76 // are to translate Drupal to English as well.
77 $existing_languages = [];
78 foreach ($languages as $langcode => $language) {
79 if (locale_is_translatable($langcode)) {
80 $existing_languages[$langcode] = $language->getName();
84 // If we have no languages available, present the list of predefined
85 // languages only. If we do have already added languages, set up two option
86 // groups with the list of existing and then predefined languages.
87 if (empty($existing_languages)) {
88 $language_options = $this->languageManager->getStandardLanguageListWithoutConfigured();
89 $default = key($language_options);
92 $default = key($existing_languages);
94 (string) $this->t('Existing languages') => $existing_languages,
95 (string) $this->t('Languages not yet added') => $this->languageManager->getStandardLanguageListWithoutConfigured(),
100 'file_validate_extensions' => ['po'],
101 'file_validate_size' => [file_upload_max_size()],
105 '#title' => $this->t('Translation file'),
107 '#theme' => 'file_upload_help',
108 '#description' => $this->t('A Gettext Portable Object file.'),
109 '#upload_validators' => $validators,
112 '#upload_validators' => $validators,
113 '#upload_location' => 'translations://',
114 '#attributes' => ['class' => ['file-import-input']],
116 $form['langcode'] = [
118 '#title' => $this->t('Language'),
119 '#options' => $language_options,
120 '#default_value' => $default,
121 '#attributes' => ['class' => ['langcode-input']],
124 $form['customized'] = [
125 '#title' => $this->t('Treat imported strings as custom translations'),
126 '#type' => 'checkbox',
128 $form['overwrite_options'] = [
129 '#type' => 'container',
132 $form['overwrite_options']['not_customized'] = [
133 '#title' => $this->t('Overwrite non-customized translations'),
134 '#type' => 'checkbox',
137 ':input[name="customized"]' => ['checked' => TRUE],
141 $form['overwrite_options']['customized'] = [
142 '#title' => $this->t('Overwrite existing customized translations'),
143 '#type' => 'checkbox',
147 '#type' => 'actions',
149 $form['actions']['submit'] = [
151 '#value' => $this->t('Import'),
159 public function validateForm(array &$form, FormStateInterface $form_state) {
160 $this->file = _file_save_upload_from_form($form['file'], $form_state, 0);
162 // Ensure we have the file uploaded.
164 $form_state->setErrorByName('file', $this->t('File to import not found.'));
171 public function submitForm(array &$form, FormStateInterface $form_state) {
172 $this->moduleHandler->loadInclude('locale', 'translation.inc');
173 // Add language, if not yet supported.
174 $language = $this->languageManager->getLanguage($form_state->getValue('langcode'));
175 if (empty($language)) {
176 $language = ConfigurableLanguage::createFromLangcode($form_state->getValue('langcode'));
178 drupal_set_message($this->t('The language %language has been created.', ['%language' => $this->t($language->label())]));
180 $options = array_merge(_locale_translation_default_update_options(), [
181 'langcode' => $form_state->getValue('langcode'),
182 'overwrite_options' => $form_state->getValue('overwrite_options'),
183 'customized' => $form_state->getValue('customized') ? LOCALE_CUSTOMIZED : LOCALE_NOT_CUSTOMIZED,
185 $this->moduleHandler->loadInclude('locale', 'bulk.inc');
186 $file = locale_translate_file_attach_properties($this->file, $options);
187 $batch = locale_translate_batch_build([$file->uri => $file], $options);
190 // Create or update all configuration translations for this language.
191 if ($batch = locale_config_batch_update_components($options, [$form_state->getValue('langcode')])) {
195 $form_state->setRedirect('locale.translate_page');