You are here

function _potx_process_file in Translation template extractor 7

Same name and namespace in other branches
  1. 8 potx.inc \_potx_process_file()
  2. 5.2 potx.inc \_potx_process_file()
  3. 5 potx.inc \_potx_process_file()
  4. 6.3 potx.inc \_potx_process_file()
  5. 6 potx.inc \_potx_process_file()
  6. 6.2 potx.inc \_potx_process_file()
  7. 7.3 potx.inc \_potx_process_file()
  8. 7.2 potx.inc \_potx_process_file()

Process a file and put extracted information to the given parameters.

Parameters

$file_path: Comlete path to file to process.

$strip_prefix: An integer denoting the number of chars to strip from filepath for output.

$save_callback: Callback function to use to save the collected strings.

$version_callback: Callback function to use to save collected version numbers.

$api_version: Drupal API version to work with.

4 calls to _potx_process_file()
potx-cli.php in ./potx-cli.php
PotxTestCase::parseFile in tests/potx.test
Parse the given file with the given API version.
potx_coder_review in ./potx.module
Callback implementation for coder review of one file.
potx_select_form_submit in ./potx.module
Generate translation template or translation file for the requested component.

File

./potx.inc, line 133
Extraction API used by the web and command line interface.

Code

function _potx_process_file($file_path, $strip_prefix = 0, $save_callback = '_potx_save_string', $version_callback = '_potx_save_version', $api_version = POTX_API_CURRENT) {
  global $_potx_tokens, $_potx_lookup;

  // Figure out the basename and extension to select extraction method.
  $basename = basename($file_path);
  $name_parts = pathinfo($basename);

  // Always grab the CVS version number from the code
  $code = file_get_contents($file_path);
  $file_name = $strip_prefix > 0 ? substr($file_path, $strip_prefix) : $file_path;
  _potx_find_version_number($code, $file_name, $version_callback);

  // The .info files are not PHP code, no need to tokenize.
  if ($name_parts['extension'] == 'info') {
    _potx_find_info_file_strings($file_path, $file_name, $save_callback, $api_version);
    return;
  }
  elseif ($name_parts['extension'] == 'js' && $api_version > POTX_API_5) {

    // @todo: D7 context support.
    _potx_parse_js_file($code, $file_name, $save_callback);
  }

  // Extract raw PHP language tokens.
  $raw_tokens = token_get_all($code);
  unset($code);

  // Remove whitespace and possible HTML (the later in templates for example),
  // count line numbers so we can include them in the output.
  $_potx_tokens = array();
  $_potx_lookup = array();
  $token_number = 0;
  $line_number = 1;
  foreach ($raw_tokens as $token) {
    if (!is_array($token) || $token[0] != T_WHITESPACE && $token[0] != T_INLINE_HTML) {
      if (is_array($token)) {
        $token[] = $line_number;

        // Fill array for finding token offsets quickly.
        if ($token[0] == T_STRING || $token[0] == T_VARIABLE && $token[1] == '$t') {
          if (!isset($_potx_lookup[$token[1]])) {
            $_potx_lookup[$token[1]] = array();
          }
          $_potx_lookup[$token[1]][] = $token_number;
        }
      }
      $_potx_tokens[] = $token;
      $token_number++;
    }

    // Collect line numbers.
    if (is_array($token)) {
      $line_number += count(explode("\n", $token[1])) - 1;
    }
    else {
      $line_number += count(explode("\n", $token)) - 1;
    }
  }
  unset($raw_tokens);

  // Regular t() calls with different usages.
  if ($api_version > POTX_API_6) {

    // Drupal 7 onwards supports context on t() and st().
    _potx_find_t_calls_with_context($file_name, $save_callback);
    _potx_find_t_calls_with_context($file_name, $save_callback, '$t', POTX_STRING_BOTH);
    _potx_find_t_calls_with_context($file_name, $save_callback, 'st', POTX_STRING_INSTALLER);
  }
  else {
    _potx_find_t_calls($file_name, $save_callback);
    _potx_find_t_calls($file_name, $save_callback, '$t', POTX_STRING_BOTH);
    _potx_find_t_calls($file_name, $save_callback, 'st', POTX_STRING_INSTALLER);
  }

  // This does not support context even in Drupal 7.
  _potx_find_t_calls($file_name, $save_callback, '_locale_import_message', POTX_STRING_BOTH);
  if ($api_version > POTX_API_5) {

    // Watchdog calls have both of their arguments translated from Drupal 6.x.
    _potx_find_watchdog_calls($file_name, $save_callback);
  }
  else {

    // Watchdog calls only have their first argument translated in Drupal 5.x
    // and before.
    _potx_find_t_calls($file_name, $save_callback, 'watchdog');
  }

  // Plurals need unique parsing.
  _potx_find_format_plural_calls($file_name, $save_callback, $api_version);
  if ($name_parts['extension'] == 'module') {
    if ($api_version < POTX_API_7) {
      _potx_find_perm_hook($file_name, $name_parts['filename'], $save_callback);
    }
    if ($api_version > POTX_API_5) {
      _potx_find_menu_hooks($file_name, $name_parts['filename'], $save_callback);
    }
  }

  // Special handling of some Drupal core files.
  if ($basename == 'locale.inc' && $api_version < POTX_API_7 || $basename == 'iso.inc') {
    _potx_find_language_names($file_name, $save_callback, $api_version);
  }
  elseif ($basename == 'locale.module') {
    _potx_add_date_strings($file_name, $save_callback, $api_version);
  }
  elseif ($basename == 'common.inc') {
    _potx_add_format_interval_strings($file_name, $save_callback, $api_version);
  }
  elseif ($basename == 'system.module') {
    _potx_add_default_region_names($file_name, $save_callback, $api_version);
  }
  elseif ($basename == 'user.module') {

    // Save default user role names.
    $save_callback('anonymous user', POTX_CONTEXT_NONE, $file_name);
    $save_callback('authenticated user', POTX_CONTEXT_NONE, $file_name);
    if ($api_version > POTX_API_6) {

      // Administator role is included by default from Drupal 7.
      $save_callback('administrator', POTX_CONTEXT_NONE, $file_name);
    }
  }
}