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')
51 * Constructs a form for language import.
53 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
54 * The module handler service.
55 * @param \Drupal\language\ConfigurableLanguageManagerInterface $language_manager
56 * The configurable language manager.
58 public function __construct(ModuleHandlerInterface $module_handler, ConfigurableLanguageManagerInterface $language_manager) {
59 $this->moduleHandler = $module_handler;
60 $this->languageManager = $language_manager;
66 public function getFormId() {
67 return 'locale_translate_import_form';
71 * Form constructor for the translation import screen.
73 public function buildForm(array $form, FormStateInterface $form_state) {
74 $languages = $this->languageManager->getLanguages();
76 // Initialize a language list to the ones available, including English if we
77 // are to translate Drupal to English as well.
78 $existing_languages = [];
79 foreach ($languages as $langcode => $language) {
80 if (locale_is_translatable($langcode)) {
81 $existing_languages[$langcode] = $language->getName();
85 // If we have no languages available, present the list of predefined
86 // languages only. If we do have already added languages, set up two option
87 // groups with the list of existing and then predefined languages.
88 if (empty($existing_languages)) {
89 $language_options = $this->languageManager->getStandardLanguageListWithoutConfigured();
90 $default = key($language_options);
93 $default = key($existing_languages);
95 (string) $this->t('Existing languages') => $existing_languages,
96 (string) $this->t('Languages not yet added') => $this->languageManager->getStandardLanguageListWithoutConfigured(),
101 'file_validate_extensions' => ['po'],
102 'file_validate_size' => [file_upload_max_size()],
106 '#title' => $this->t('Translation file'),
108 '#theme' => 'file_upload_help',
109 '#description' => $this->t('A Gettext Portable Object file.'),
110 '#upload_validators' => $validators,
113 '#upload_validators' => $validators,
114 '#upload_location' => 'translations://',
115 '#attributes' => ['class' => ['file-import-input']],
117 $form['langcode'] = [
119 '#title' => $this->t('Language'),
120 '#options' => $language_options,
121 '#default_value' => $default,
122 '#attributes' => ['class' => ['langcode-input']],
125 $form['customized'] = [
126 '#title' => $this->t('Treat imported strings as custom translations'),
127 '#type' => 'checkbox',
129 $form['overwrite_options'] = [
130 '#type' => 'container',
133 $form['overwrite_options']['not_customized'] = [
134 '#title' => $this->t('Overwrite non-customized translations'),
135 '#type' => 'checkbox',
138 ':input[name="customized"]' => ['checked' => TRUE],
142 $form['overwrite_options']['customized'] = [
143 '#title' => $this->t('Overwrite existing customized translations'),
144 '#type' => 'checkbox',
148 '#type' => 'actions',
150 $form['actions']['submit'] = [
152 '#value' => $this->t('Import'),
160 public function validateForm(array &$form, FormStateInterface $form_state) {
161 $this->file = _file_save_upload_from_form($form['file'], $form_state, 0);
163 // Ensure we have the file uploaded.
165 $form_state->setErrorByName('file', $this->t('File to import not found.'));
172 public function submitForm(array &$form, FormStateInterface $form_state) {
173 $this->moduleHandler->loadInclude('locale', 'translation.inc');
174 // Add language, if not yet supported.
175 $language = $this->languageManager->getLanguage($form_state->getValue('langcode'));
176 if (empty($language)) {
177 $language = ConfigurableLanguage::createFromLangcode($form_state->getValue('langcode'));
179 $this->messenger()->addStatus($this->t('The language %language has been created.', ['%language' => $this->t($language->label())]));
181 $options = array_merge(_locale_translation_default_update_options(), [
182 'langcode' => $form_state->getValue('langcode'),
183 'overwrite_options' => $form_state->getValue('overwrite_options'),
184 'customized' => $form_state->getValue('customized') ? LOCALE_CUSTOMIZED : LOCALE_NOT_CUSTOMIZED,
186 $this->moduleHandler->loadInclude('locale', 'bulk.inc');
187 $file = locale_translate_file_attach_properties($this->file, $options);
188 $batch = locale_translate_batch_build([$file->uri => $file], $options);
191 // Create or update all configuration translations for this language.
192 if ($batch = locale_config_batch_update_components($options, [$form_state->getValue('langcode')])) {
196 $form_state->setRedirect('locale.translate_page');