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.
15 class ImportForm extends FormBase {
18 * Uploaded file entity.
20 * @var \Drupal\file\Entity\File
25 * The module handler service.
27 * @var \Drupal\Core\Extension\ModuleHandlerInterface
29 protected $moduleHandler;
32 * The configurable language manager.
34 * @var \Drupal\language\ConfigurableLanguageManagerInterface
36 protected $languageManager;
41 public static function create(ContainerInterface $container) {
43 $container->get('module_handler'),
44 $container->get('language_manager')
48 * Constructs a form for language import.
50 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
51 * The module handler service.
52 * @param \Drupal\language\ConfigurableLanguageManagerInterface $language_manager
53 * The configurable language manager.
55 public function __construct(ModuleHandlerInterface $module_handler, ConfigurableLanguageManagerInterface $language_manager) {
56 $this->moduleHandler = $module_handler;
57 $this->languageManager = $language_manager;
63 public function getFormId() {
64 return 'locale_translate_import_form';
68 * Form constructor for the translation import screen.
70 public function buildForm(array $form, FormStateInterface $form_state) {
71 $languages = $this->languageManager->getLanguages();
73 // Initialize a language list to the ones available, including English if we
74 // are to translate Drupal to English as well.
75 $existing_languages = [];
76 foreach ($languages as $langcode => $language) {
77 if (locale_is_translatable($langcode)) {
78 $existing_languages[$langcode] = $language->getName();
82 // If we have no languages available, present the list of predefined
83 // languages only. If we do have already added languages, set up two option
84 // groups with the list of existing and then predefined languages.
85 if (empty($existing_languages)) {
86 $language_options = $this->languageManager->getStandardLanguageListWithoutConfigured();
87 $default = key($language_options);
90 $default = key($existing_languages);
92 (string) $this->t('Existing languages') => $existing_languages,
93 (string) $this->t('Languages not yet added') => $this->languageManager->getStandardLanguageListWithoutConfigured(),
98 'file_validate_extensions' => ['po'],
99 'file_validate_size' => [file_upload_max_size()],
103 '#title' => $this->t('Translation file'),
105 '#theme' => 'file_upload_help',
106 '#description' => $this->t('A Gettext Portable Object file.'),
107 '#upload_validators' => $validators,
110 '#upload_validators' => $validators,
111 '#attributes' => ['class' => ['file-import-input']],
113 $form['langcode'] = [
115 '#title' => $this->t('Language'),
116 '#options' => $language_options,
117 '#default_value' => $default,
118 '#attributes' => ['class' => ['langcode-input']],
121 $form['customized'] = [
122 '#title' => $this->t('Treat imported strings as custom translations'),
123 '#type' => 'checkbox',
125 $form['overwrite_options'] = [
126 '#type' => 'container',
129 $form['overwrite_options']['not_customized'] = [
130 '#title' => $this->t('Overwrite non-customized translations'),
131 '#type' => 'checkbox',
134 ':input[name="customized"]' => ['checked' => TRUE],
138 $form['overwrite_options']['customized'] = [
139 '#title' => $this->t('Overwrite existing customized translations'),
140 '#type' => 'checkbox',
144 '#type' => 'actions',
146 $form['actions']['submit'] = [
148 '#value' => $this->t('Import'),
156 public function validateForm(array &$form, FormStateInterface $form_state) {
157 $this->file = file_save_upload('file', $form['file']['#upload_validators'], 'translations://', 0);
159 // Ensure we have the file uploaded.
161 $form_state->setErrorByName('file', $this->t('File to import not found.'));
168 public function submitForm(array &$form, FormStateInterface $form_state) {
169 $this->moduleHandler->loadInclude('locale', 'translation.inc');
170 // Add language, if not yet supported.
171 $language = $this->languageManager->getLanguage($form_state->getValue('langcode'));
172 if (empty($language)) {
173 $language = ConfigurableLanguage::createFromLangcode($form_state->getValue('langcode'));
175 drupal_set_message($this->t('The language %language has been created.', ['%language' => $this->t($language->label())]));
177 $options = array_merge(_locale_translation_default_update_options(), [
178 'langcode' => $form_state->getValue('langcode'),
179 'overwrite_options' => $form_state->getValue('overwrite_options'),
180 'customized' => $form_state->getValue('customized') ? LOCALE_CUSTOMIZED : LOCALE_NOT_CUSTOMIZED,
182 $this->moduleHandler->loadInclude('locale', 'bulk.inc');
183 $file = locale_translate_file_attach_properties($this->file, $options);
184 $batch = locale_translate_batch_build([$file->uri => $file], $options);
187 // Create or update all configuration translations for this language.
188 if ($batch = locale_config_batch_update_components($options, [$form_state->getValue('langcode')])) {
192 $form_state->setRedirect('locale.translate_page');