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