3 namespace Drush\Drupal\Commands\core;
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;
13 class LanguageCommands extends DrushCommands
17 * @var \Drupal\Core\Language\LanguageManagerInterface
19 protected $languageManager;
22 * @var \Drupal\Core\Extension\ModuleHandlerInterface
24 protected $moduleHandler;
27 * @return \Drupal\Core\Language\LanguageManagerInterface
29 public function getLanguageManager()
31 return $this->languageManager;
35 * @return \Drupal\Core\Extension\ModuleHandlerInterface
37 public function getModuleHandler()
39 return $this->moduleHandler;
42 public function __construct(LanguageManagerInterface $languageManager, ModuleHandlerInterface $moduleHandler)
44 $this->languageManager = $languageManager;
45 $this->moduleHandler = $moduleHandler;
49 * Add a configurable language.
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
63 public function add($langcode, $options = ['skip-translations' => false])
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)) {
73 foreach ($langcodes as $langcode) {
74 $language = ConfigurableLanguage::createFromLangcode($langcode);
77 $this->logger->success(dt('Added language @language', [
78 '@language' => $language->label(),
82 if ($options['skip-translations']) {
86 if ($this->getModuleHandler()->moduleExists('locale')) {
87 $this->setBatchLanguageImport($langcodes);
88 drush_backend_batch_process();
94 * Print the currently available languages.
96 * @command language:info
97 * @aliases language-info
101 * direction: Direction
104 * @default-fields language,direction,default
105 * @return \Consolidation\OutputFormatters\StructuredData\RowsOfFields
107 public function info()
110 $languages = $this->getLanguageManager()->getLanguages();
112 foreach ($languages as $key => $language) {
114 'language' => $language->getName() . ' (' . $language->getId() . ')',
115 'direction' => $language->getDirection(),
116 'default' => $language->isDefault() ? dt('yes') : '',
117 'locked' => $language->isLocked() ? dt('yes') : '',
122 return new RowsOfFields($rows);
126 * Filters valid language codes.
131 * Exception when a language code is not in the standard language list.
133 private function filterValidLangcode($langcodes)
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
148 * Filters new language codes.
153 private function filterNewLangcode($langcodes)
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
161 unset($langcodes[$key]);
169 * Sets a batch to download and import translations and update configurations.
173 private function setBatchLanguageImport($langcodes)
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();
181 locale_translation_clear_status();
182 $batch = locale_translation_batch_update_build([], $langcodes, $translationOptions);
184 if ($batch = locale_config_batch_update_components($translationOptions, $langcodes)) {