3 namespace Drush\Drupal\Commands\core;
5 use Drupal\Core\Extension\ModuleHandlerInterface;
6 use Drupal\Core\State\StateInterface;
7 use Drush\Commands\DrushCommands;
9 class LocaleCommands extends DrushCommands
12 protected $moduleHandler;
17 * @return \Drupal\Core\Extension\ModuleHandlerInterface
19 public function getModuleHandler()
21 return $this->moduleHandler;
27 public function getState()
32 public function __construct(ModuleHandlerInterface $moduleHandler, StateInterface $state)
34 $this->moduleHandler = $moduleHandler;
35 $this->state = $state;
39 * Checks for available translation updates.
41 * @command locale:check
42 * @aliases locale-check
43 * @validate-module-enabled locale
45 public function check()
47 $this->getModuleHandler()->loadInclude('locale', 'inc', 'locale.compare');
49 // Check translation status of all translatable project in all languages.
50 // First we clear the cached list of projects. Although not strictly
51 // necessary, this is helpful in case the project list is out of sync.
52 locale_translation_flush_projects();
53 locale_translation_check_projects();
55 // Execute a batch if required. A batch is only used when remote files
58 drush_backend_batch_process();
63 * Imports the available translation updates.
65 * @see TranslationStatusForm::buildForm()
66 * @see TranslationStatusForm::prepareUpdateData()
67 * @see TranslationStatusForm::submitForm()
69 * @todo This can be simplified once https://www.drupal.org/node/2631584 lands
72 * @command locale:update
73 * @aliases locale-update
74 * @option langcodes A comma-separated list of language codes to update. If omitted, all translations will be updated.
75 * @validate-module-enabled locale
77 public function update($options = ['langcodes' => self::REQ])
79 $module_handler = $this->getModuleHandler();
80 $module_handler->loadInclude('locale', 'fetch.inc');
81 $module_handler->loadInclude('locale', 'bulk.inc');
84 foreach (locale_translation_get_status() as $project_id => $project) {
85 foreach ($project as $langcode => $project_info) {
86 if (!empty($project_info->type) && !in_array($langcode, $langcodes)) {
87 $langcodes[] = $langcode;
92 if ($passed_langcodes = $translationOptions['langcodes']) {
93 $langcodes = array_intersect($langcodes, explode(',', $passed_langcodes));
94 // @todo Not selecting any language code in the user interface results in
95 // all translations being updated, so we mimick that behavior here.
98 // Deduplicate the list of langcodes since each project may have added the
99 // same language several times.
100 $langcodes = array_unique($langcodes);
102 // @todo Restricting by projects is not possible in the user interface and is
103 // broken when attempting to do it in a hook_form_alter() implementation so
104 // we do not allow for it here either.
107 // Set the translation import options. This determines if existing
108 // translations will be overwritten by imported strings.
109 $translationOptions = _locale_translation_default_update_options();
111 // If the status was updated recently we can immediately start fetching the
112 // translation updates. If the status is expired we clear it an run a batch to
113 // update the status and then fetch the translation updates.
114 $last_checked = $this->getState()->get('locale.translation_last_checked');
115 if ($last_checked < REQUEST_TIME - LOCALE_TRANSLATION_STATUS_TTL) {
116 locale_translation_clear_status();
117 $batch = locale_translation_batch_update_build([], $langcodes, $translationOptions);
120 // Set a batch to download and import translations.
121 $batch = locale_translation_batch_fetch_build($projects, $langcodes, $translationOptions);
123 // Set a batch to update configuration as well.
124 if ($batch = locale_config_batch_update_components($translationOptions, $langcodes)) {
129 drush_backend_batch_process();