191562c3b6148f04a27bc192c5c53cb1ccafe19a
[yaffs-website] / vendor / drush / drush / src / Drupal / Commands / core / LanguageCommands.php
1 <?php
2
3 namespace Drush\Drupal\Commands\core;
4
5 use Consolidation\OutputFormatters\StructuredData\RowsOfFields;
6 use Drupal\Core\Extension\ModuleHandlerInterface;
7 use Drupal\Core\Language\LanguageInterface;
8 use Drupal\Core\Language\LanguageManagerInterface;
9 use Drupal\language\Entity\ConfigurableLanguage;
10 use Drush\Commands\DrushCommands;
11 use Drush\Utils\StringUtils;
12
13 class LanguageCommands extends DrushCommands
14 {
15
16     /**
17      * @var \Drupal\Core\Language\LanguageManagerInterface
18      */
19     protected $languageManager;
20
21     /**
22      * @var \Drupal\Core\Extension\ModuleHandlerInterface
23      */
24     protected $moduleHandler;
25
26     /**
27      * @return \Drupal\Core\Language\LanguageManagerInterface
28      */
29     public function getLanguageManager()
30     {
31         return $this->languageManager;
32     }
33
34     /**
35      * @return \Drupal\Core\Extension\ModuleHandlerInterface
36      */
37     public function getModuleHandler()
38     {
39         return $this->moduleHandler;
40     }
41
42     public function __construct(LanguageManagerInterface $languageManager, ModuleHandlerInterface $moduleHandler)
43     {
44         $this->languageManager = $languageManager;
45         $this->moduleHandler = $moduleHandler;
46     }
47
48     /**
49      * Add a configurable language.
50      *
51      * @command language:add
52      * @param $langcode A comma delimited list of language codes.
53      * @option skip-translations Prevent translations to be downloaded and/or imported.
54      * @usage drush language:add nl,fr
55      *   Add Dutch and French language and import their translations.
56      * @usage drush language:add nl --skip-translations
57      *   Add Dutch language without importing translations.
58      * @aliases language-add
59      * @validate-module-enabled language
60      * @hidden
61      * @throws \Exception
62      */
63     public function add($langcode, $options = ['skip-translations' => false])
64     {
65         if ($langcodes = StringUtils::csvToArray($langcode)) {
66             $langcodes = array_unique($langcodes);
67             $langcodes = $this->filterValidLangcode($langcodes);
68             $langcodes = $this->filterNewLangcode($langcodes);
69             if (empty($langcodes)) {
70                 return;
71             }
72
73             foreach ($langcodes as $langcode) {
74                 $language = ConfigurableLanguage::createFromLangcode($langcode);
75                 $language->save();
76
77                 $this->logger->success(dt('Added language @language', [
78                     '@language' => $language->label(),
79                 ]));
80             }
81
82             if ($options['skip-translations']) {
83                 return;
84             }
85
86             if ($this->getModuleHandler()->moduleExists('locale')) {
87                 $this->setBatchLanguageImport($langcodes);
88                 drush_backend_batch_process();
89             }
90         }
91     }
92
93     /**
94      * Print the currently available languages.
95      *
96      * @command language:info
97      * @aliases language-info
98      * @hidden
99      * @field-labels
100      *   language: Language
101      *   direction: Direction
102      *   default: Default
103      *   locked: Locked
104      * @default-fields language,direction,default
105      * @return \Consolidation\OutputFormatters\StructuredData\RowsOfFields
106      */
107     public function info()
108     {
109         $rows = [];
110         $languages = $this->getLanguageManager()->getLanguages();
111
112         foreach ($languages as $key => $language) {
113             $row = [
114                 'language' => $language->getName() . ' (' . $language->getId() . ')',
115                 'direction' => $language->getDirection(),
116                 'default' => $language->isDefault() ? dt('yes') : '',
117                 'locked' => $language->isLocked() ? dt('yes') : '',
118             ];
119             $rows[$key] = $row;
120         }
121
122         return new RowsOfFields($rows);
123     }
124
125     /**
126      * Filters valid language codes.
127      *
128      * @param $langcodes
129      * @return array
130      * @throws \Exception
131      *   Exception when a language code is not in the standard language list.
132      */
133     private function filterValidLangcode($langcodes)
134     {
135         $standardLanguages = $this->getLanguageManager()->getStandardLanguageList();
136         foreach ($langcodes as $key => $langcode) {
137             if (!isset($standardLanguages[$langcode])) {
138                 throw new \Exception(dt('Unknown language: !langcode', [
139                     '!langcode' => $langcode
140                 ]));
141             }
142         }
143
144         return $langcodes;
145     }
146
147     /**
148      * Filters new language codes.
149      *
150      * @param $langcodes
151      * @return array
152      */
153     private function filterNewLangcode($langcodes)
154     {
155         $enabledLanguages = $this->getLanguageManager()->getLanguages();
156         foreach ($langcodes as $key => $langcode) {
157             if (isset($enabledLanguages[$langcode])) {
158                 $this->logger->warning(dt('The language !langcode is already enabled.', [
159                     '!langcode' => $langcode
160                 ]));
161                 unset($langcodes[$key]);
162             }
163         }
164
165         return $langcodes;
166     }
167
168     /**
169      * Sets a batch to download and import translations and update configurations.
170      *
171      * @param $langcodes
172      */
173     private function setBatchLanguageImport($langcodes)
174     {
175         $moduleHandler = $this->getModuleHandler();
176         $moduleHandler->loadInclude('locale', 'inc', 'locale.translation');
177         $moduleHandler->loadInclude('locale', 'inc', 'locale.fetch');
178         $moduleHandler->loadInclude('locale', 'inc', 'locale.bulk');
179         $translationOptions = _locale_translation_default_update_options();
180
181         locale_translation_clear_status();
182         $batch = locale_translation_batch_update_build([], $langcodes, $translationOptions);
183         batch_set($batch);
184         if ($batch = locale_config_batch_update_components($translationOptions, $langcodes)) {
185             batch_set($batch);
186         }
187     }
188 }