You are here

class PotxCommands in Translation template extractor 8

Class PotxCommands.

Translation template extractor module drush integration.

@package Drupal\potx\Commands

Hierarchy

  • class \Drupal\potx\Commands\PotxCommands extends \Drush\Commands\DrushCommands

Expanded class hierarchy of PotxCommands

1 string reference to 'PotxCommands'
drush.services.yml in ./drush.services.yml
drush.services.yml
1 service uses PotxCommands
potx.commands in ./drush.services.yml
\Drupal\potx\Commands\PotxCommands

File

src/Commands/PotxCommands.php, line 15

Namespace

Drupal\potx\Commands
View source
class PotxCommands extends DrushCommands {

  /**
   * Extract translatable strings from Drupal source code.
   *
   * @param string $mode
   *   The output mode e.g. single multiple core.
   * @param array $options
   *   An associative array of options whose values come from cli, aliases,
   *    config, etc.
   *
   * @command potx
   *
   * @option modules Comma delimited list of modules to extract translatable strings from.
   * @option files Comma delimited list of files to extract translatable strings from.
   * @option folder Folder to begin translation extraction in. When no other option is set this defaults to current directory.
   * @option api Drupal core version to use for extraction settings.
   * @option language Language code if the template should have language dependent content (like plural formulas and language name) included.
   * @option translations Enable translations export.
   * @usage potx single Extract translatable strings from applicable files in current directory and write to single output file.
   * @usage potx multiple --modules=example Extract translatable strings from applicable files of example module and write to module-specific output file.
   * @usage potx --files=sites/all/modules/example/example.module Extract translatable strings from example.module and write to single output file.
   * @usage potx single --api=8 --folder=projects/drupal/8 Extract strings from folder projects/drupal/8 using API version 8.
   * @aliases
   * @field-labels
   *   files: Files
   *   strings: Strings
   *   warnings: Warnings
   *
   * @return \Consolidation\OutputFormatters\StructuredData\RowsOfFields
   *   The number of files, strings, and errors processed, in a table format.
   */
  public function potx($mode = NULL, array $options = [
    'modules' => NULL,
    'files' => NULL,
    'folder' => NULL,
    'api' => NULL,
    'language' => NULL,
    'translations' => FALSE,
  ]) {

    // 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 = $options['modules'];
    $files_option = $options['files'];
    $folder_option = $options['folder'];
    $api_option = $options['api'];
    if (empty($api_option) || !in_array($api_option, [
      5,
      6,
      7,
      8,
    ])) {
      $api_option = POTX_API_CURRENT;
    }
    $language_option = $options['language'];
    if (!empty($language_option) && !in_array($language_option, array_keys(\Drupal::languageManager()
      ->getLanguages()))) {
      $language_option = NULL;
    }
    $translations_option = (bool) $options['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(getcwd() . '/', '*', $api_option, TRUE);
    }
    foreach ($files as $file) {
      $this
        ->output()
        ->writeln(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();
    $this
      ->output()
      ->writeln("");

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

    // Get saved strings.
    $strings = _potx_save_string(NULL, NULL, NULL, 0, POTX_STRING_RUNTIME);
    $rows[] = [
      'files' => count($files),
      'strings' => count($strings),
      'errors' => count($errors),
    ];
    if (!empty($errors)) {
      $this
        ->output()
        ->writeln(dt("Errors"));
      foreach ($errors as $error) {
        $this
          ->logger()
          ->error($error);
      }
    }
    return new RowsOfFields($rows);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PotxCommands::potx public function Extract translatable strings from Drupal source code.