X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=vendor%2Fdrush%2Fdrush%2Fsrc%2FDrupal%2FCommands%2Fcore%2FLanguageCommands.php;fp=vendor%2Fdrush%2Fdrush%2Fsrc%2FDrupal%2FCommands%2Fcore%2FLanguageCommands.php;h=191562c3b6148f04a27bc192c5c53cb1ccafe19a;hp=0000000000000000000000000000000000000000;hb=0bf8d09d2542548982e81a441b1f16e75873a04f;hpb=74df008bdbb3a11eeea356744f39b802369bda3c diff --git a/vendor/drush/drush/src/Drupal/Commands/core/LanguageCommands.php b/vendor/drush/drush/src/Drupal/Commands/core/LanguageCommands.php new file mode 100644 index 000000000..191562c3b --- /dev/null +++ b/vendor/drush/drush/src/Drupal/Commands/core/LanguageCommands.php @@ -0,0 +1,188 @@ +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 + * @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); + } + } +}