locale.d8.drush.inc

Provides Drush commands related to Interface Translation.

Functions

Namesort descending Description
drush_locale_check Checks for available translation updates.
drush_locale_update Imports the available translation updates.
locale_drush_command Implementation of hook_drush_command().
locale_drush_help Implementation of hook_drush_help().

File

commands/core/locale.d8.drush.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Provides Drush commands related to Interface Translation.
  5. */
  6. /**
  7. * Implementation of hook_drush_help().
  8. */
  9. function locale_drush_help($section) {
  10. switch ($section) {
  11. case 'meta:locale:title':
  12. return dt('Interface translation');
  13. case 'meta:locale:summary':
  14. return dt('Interact with the interface translation system.');
  15. }
  16. }
  17. /**
  18. * Implementation of hook_drush_command().
  19. */
  20. function locale_drush_command() {
  21. $items['locale-check'] = [
  22. 'description' => 'Checks for available translation updates.',
  23. 'drupal dependencies' => ['locale'],
  24. 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
  25. ];
  26. $items['locale-update'] = [
  27. 'description' => 'Updates the available translations.',
  28. 'drupal dependencies' => ['locale'],
  29. 'options' => [
  30. 'langcodes' => 'A comma-separated list of language codes to update. If omitted, all translations will be updated.'
  31. ],
  32. 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
  33. ];
  34. // @todo Implement proper export and import commands.
  35. return $items;
  36. }
  37. /**
  38. * Checks for available translation updates.
  39. *
  40. * @see \Drupal\locale\Controller\LocaleController::checkTranslation()
  41. *
  42. * @todo This can be simplified once https://www.drupal.org/node/2631584 lands
  43. * in Drupal core.
  44. */
  45. function drush_locale_check() {
  46. \Drupal::moduleHandler()->loadInclude('locale', 'inc', 'locale.compare');
  47. // Check translation status of all translatable project in all languages.
  48. // First we clear the cached list of projects. Although not strictly
  49. // necessary, this is helpful in case the project list is out of sync.
  50. locale_translation_flush_projects();
  51. locale_translation_check_projects();
  52. // Execute a batch if required. A batch is only used when remote files
  53. // are checked.
  54. if (batch_get()) {
  55. drush_backend_batch_process();
  56. }
  57. }
  58. /**
  59. * Imports the available translation updates.
  60. *
  61. * @see TranslationStatusForm::buildForm()
  62. * @see TranslationStatusForm::prepareUpdateData()
  63. * @see TranslationStatusForm::submitForm()
  64. *
  65. * @todo This can be simplified once https://www.drupal.org/node/2631584 lands
  66. * in Drupal core.
  67. */
  68. function drush_locale_update() {
  69. $module_handler = \Drupal::moduleHandler();
  70. $module_handler->loadInclude('locale', 'fetch.inc');
  71. $module_handler->loadInclude('locale', 'bulk.inc');
  72. $langcodes = [];
  73. foreach (locale_translation_get_status() as $project_id => $project) {
  74. foreach ($project as $langcode => $project_info) {
  75. if (!empty($project_info->type) && !in_array($langcode, $langcodes)) {
  76. $langcodes[] = $langcode;
  77. }
  78. }
  79. }
  80. if ($passed_langcodes = drush_get_option('langcodes')) {
  81. $langcodes = array_intersect($langcodes, explode(',', $passed_langcodes));
  82. // @todo Not selecting any language code in the user interface results in
  83. // all translations being updated, so we mimick that behavior here.
  84. }
  85. // @todo Restricting by projects is not possible in the user interface and is
  86. // broken when attempting to do it in a hook_form_alter() implementation so
  87. // we do not allow for it here either.
  88. $projects = [];
  89. // Set the translation import options. This determines if existing
  90. // translations will be overwritten by imported strings.
  91. $options = _locale_translation_default_update_options();
  92. // If the status was updated recently we can immediately start fetching the
  93. // translation updates. If the status is expired we clear it an run a batch to
  94. // update the status and then fetch the translation updates.
  95. $last_checked = \Drupal::state()->get('locale.translation_last_checked');
  96. if ($last_checked < REQUEST_TIME - LOCALE_TRANSLATION_STATUS_TTL) {
  97. locale_translation_clear_status();
  98. $batch = locale_translation_batch_update_build(array(), $langcodes, $options);
  99. batch_set($batch);
  100. }
  101. else {
  102. // Set a batch to download and import translations.
  103. $batch = locale_translation_batch_fetch_build($projects, $langcodes, $options);
  104. batch_set($batch);
  105. // Set a batch to update configuration as well.
  106. if ($batch = locale_config_batch_update_components($options, $langcodes)) {
  107. batch_set($batch);
  108. }
  109. }
  110. drush_backend_batch_process();
  111. }