You are here

function _potx_process_file in Translation template extractor 6

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.2 potx.inc \_potx_process_file()
  6. 7.3 potx.inc \_potx_process_file()
  7. 7 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.

2 calls to _potx_process_file()
potx-cli.php in ./potx-cli.php
potx_select_form_submit in ./potx.module
Generate translation template or translation file for the requested module.

File

./potx.inc, line 109
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_6) {
  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) {
    _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(split("\n", $token[1])) - 1;
    }
    else {
      $line_number += count(split("\n", $token)) - 1;
    }
  }
  unset($raw_tokens);

  // Regular t() calls with different usages.
  _potx_find_t_calls($file_name, $save_callback);
  _potx_find_t_calls($file_name, $save_callback, '_locale_import_message', POTX_STRING_BOTH);
  _potx_find_t_calls($file_name, $save_callback, '$t', POTX_STRING_BOTH);
  _potx_find_t_calls($file_name, $save_callback, 'st', POTX_STRING_INSTALLER);
  if ($api_version > POTX_API_5) {

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

    // Watchdog calls only have their first argument translated in Drupal 5.x.
    _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') {
    _potx_find_perm_hook($file_name, $name_parts['filename'], $save_callback);
    if ($api_version > POTX_API_5) {
      _potx_find_menu_hook($file_name, $name_parts['filename'], $save_callback);
    }

    // Add the machine readable module name.
    $save_callback($name_parts['filename'], $file_name);
  }
  elseif ($name_parts['extension'] == 'theme') {

    // Add the theme name.
    $save_callback($name_parts['filename'], $file_name);
  }

  // Special handling of some Drupal core files.
  if ($basename == 'locale.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);
  }
  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', $file_name);
    $save_callback('authenticated user', $file_name);
  }
}