You are here

potx.drush.inc in Translation template extractor 8

Same filename and directory in other branches
  1. 6.3 potx.drush.inc
  2. 7.3 potx.drush.inc
  3. 7.2 potx.drush.inc

Translation template extractor module drush integration.

File

potx.drush.inc
View source
<?php

/**
 * @file
 * Translation template extractor module drush integration.
 */

/**
 * Implements hook_drush_command().
 *
 * @see drush_parse_command()
 */
function potx_drush_command() {
  $items['potx'] = [
    'callback' => 'potx_drush_extract',
    'description' => 'Extract translatable strings from Drupal source code.',
    'arguments' => [
      'mode' => 'potx output mode e.g. single multiple core',
    ],
    'options' => [
      'modules' => 'Comma delimited list of modules to extract translatable strings from.',
      'files' => 'Comma delimited list of files to extract translatable strings from.',
      'folder' => 'Folder to begin translation extraction in. When no other option is set this defaults to current directory.',
      'api' => 'Drupal core version to use for extraction settings.',
      'language' => 'Language code if the template should have language dependent content (like plural formulas and language name) included.',
      'translations' => 'Enable translations export.',
    ],
    'examples' => [
      'potx single' => 'Extract translatable strings from applicable files in current directory and write to single output file',
      'potx multiple --modules=example' => 'Extract translatable strings from applicable files of example module and write to module-specific output file.',
      'potx --files=sites/all/modules/example/example.module' => 'Extract translatable strings from example.module and write to single output file.',
      'potx single --api=8 --folder=projects/drupal/8' => 'Extract strings from folder projects/drupal/8 using API version 8.',
    ],
    'bootstrap' => DRUSH_BOOTSTRAP_NONE,
  ];
  return $items;
}

/**
 * Implements hook_drush_help().
 *
 * This function is called whenever a drush user calls
 * 'drush help potx'.
 */
function potx_drush_help($section) {
  if ($section == 'drush:potx') {
    $help = dt('Generates translation templates from the given Drupal source code in the current working directory.');
    $help .= "\n\n" . dt('Possible potx modes are:');
    $help .= "\n" . dt(' single    Single file output mode, every file folded into the single output file (default).');
    $help .= "\n" . dt(' multiple  Multiple file output mode, .info files folded into module .pot files.');
    $help .= "\n" . dt(' core      Drupal core output mode, .info files folded into general.pot.');
    $help .= "\n\n" . dt('If no files are specified, potx will autodiscover files from the current working directory. You can specify concrete files to look at to limit the scope of the operation.');
    return $help;
  }
}

/**
 * Drush command callback.
 */
function potx_drush_extract($mode = NULL) {

  // Include library.
  include_once __DIR__ . '/potx.inc';
  include_once __DIR__ . '/potx.local.inc';
  $files = [];
  $build_mode = POTX_BUILD_SINGLE;
  if (!is_null($mode) && in_array($mode, [
    'core',
    'multiple',
    'single',
  ])) {

    // First argument could be any of the mode names.
    $build_mode = constant('POTX_BUILD_' . strtoupper($mode));
  }

  // Silence error message reporting. Messages will be reported by at the end.
  potx_status('set', POTX_STATUS_SILENT);

  // Get Drush options.
  $modules_option = drush_get_option('modules');
  $files_option = drush_get_option('files');
  $folder_option = drush_get_option('folder');
  $api_option = drush_get_option('api');
  if (empty($api_option) || !in_array($api_option, [
    5,
    6,
    7,
    8,
  ])) {
    $api_option = POTX_API_CURRENT;
  }
  $language_option = drush_get_option('language');
  if (!empty($language_option) && !in_array($language_option, array_keys(\Drupal::languageManager()
    ->getLanguages()))) {
    $language_option = NULL;
  }
  $translations_option = (bool) drush_get_option('translations');
  potx_local_init($folder_option);
  if (!empty($modules_option)) {
    $modules = explode(',', $modules_option);
    foreach ($modules as $module) {
      $files = array_merge($files, _potx_explore_dir(drupal_get_path('module', $module) . '/', '*', $api_option, TRUE));
    }
  }
  elseif (!empty($files_option)) {
    $files = explode(',', $files_option);
  }
  elseif (!empty($folder_option)) {
    $files = _potx_explore_dir($folder_option, '*', $api_option, TRUE);
  }
  else {

    // No file list provided so autodiscover files in current directory.
    $files = _potx_explore_dir(drush_cwd() . '/', '*', $api_option, TRUE);
  }
  foreach ($files as $file) {
    drush_print(dt("Processing {$file}..."));
    _potx_process_file($file, 0, '_potx_save_string', '_potx_save_version', $api_option);
  }
  potx_finish_processing('_potx_save_string', $api_option);
  $template_export_langcode = $language_option;
  $translation_export_langcode = $translations_option && $language_option ? $language_option : NULL;
  _potx_build_files(POTX_STRING_RUNTIME, $build_mode, 'general', '_potx_save_string', '_potx_save_version', '_potx_get_header', $template_export_langcode, $translation_export_langcode, $api_option);
  _potx_build_files(POTX_STRING_INSTALLER, POTX_BUILD_SINGLE, 'installer', '_potx_save_string', '_potx_save_version', '_potx_get_header', $template_export_langcode, $translation_export_langcode, $api_option);
  _potx_write_files();
  drush_print("");
  drush_print(dt("Stats"));
  $header = [
    'files' => dt('Files'),
    'strings' => dt('Strings'),
    'warnings' => dt('Warnings'),
  ];
  $rows = [
    array_values($header),
  ];

  // Get errors, if any.
  $errors = potx_status('get');

  // Get saved strings.
  $strings = _potx_save_string(NULL, NULL, NULL, 0, POTX_STRING_RUNTIME);
  $rows[] = [
    count($files),
    count($strings),
    count($errors),
  ];
  drush_print_table($rows, TRUE);
  if (!empty($errors)) {
    drush_print(dt("Errors"));
    foreach ($errors as $error) {
      drush_set_error($error);
    }
  }
  drush_print("");
  drush_print(dt("Done"));
}

Functions

Namesort descending Description
potx_drush_command Implements hook_drush_command().
potx_drush_extract Drush command callback.
potx_drush_help Implements hook_drush_help().