You are here

function _potx_build_files in Translation template extractor 7.2

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

Creates complete file strings with _potx_store()

Parameters

$string_mode: Strings to generate files for: POTX_STRING_RUNTIME or POTX_STRING_INSTALLER.

$build_mode: Storage mode used: single, multiple or core

$force_name: Forces a given file name to get used, if single mode is on, without extension

$save_callback: Callback used to save strings previously.

$version_callback: Callback used to save versions previously.

$header_callback: Callback to invoke to get the POT header.

$template_export_langcode: Language code if the template should have language dependent content (like plural formulas and language name) included.

$translation_export_langcode: Language code if translations should also be exported.

$api_version: Drupal API version to work with.

4 calls to _potx_build_files()
PotxTestCase::buildOutput in tests/potx.test
Build the output from parsed files.
PotxTestCase::parseFile in tests/potx.test
Parse the given file with the given API version.
potx_drush_extract in ./potx.drush.inc
Drush command callback.
potx_select_component_form_submit in ./potx.admin.inc
Generate translation template or translation file for the requested component.

File

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

Code

function _potx_build_files($string_mode = POTX_STRING_RUNTIME, $build_mode = POTX_BUILD_SINGLE, $force_name = 'general', $save_callback = '_potx_save_string', $version_callback = '_potx_save_version', $header_callback = '_potx_get_header', $template_export_langcode = NULL, $translation_export_langcode = NULL, $api_version = POTX_API_CURRENT) {
  global $_potx_store;

  // Get strings and versions by reference.
  $strings = $save_callback(NULL, NULL, NULL, 0, $string_mode);
  $versions = $version_callback();

  // We might not have any string recorded in this string mode.
  if (!is_array($strings)) {
    return;
  }
  foreach ($strings as $string => $string_info) {
    foreach ($string_info as $context => $file_info) {

      // Build a compact list of files this string occured in.
      $occured = $file_list = array();

      // Look for strings appearing in multiple directories (ie.
      // different subprojects). So we can include them in general.pot.
      $names = array_keys($file_info);
      $last_location = dirname(array_shift($names));
      $multiple_locations = FALSE;
      foreach ($file_info as $file => $lines) {
        $occured[] = "{$file}:" . join(';', $lines);
        if (isset($versions[$file])) {
          $file_list[] = $versions[$file];
        }
        if (dirname($file) != $last_location) {
          $multiple_locations = TRUE;
        }
        $last_location = dirname($file);
      }

      // Mark duplicate strings (both translated in the app and in the installer).
      $comment = join(" ", $occured);
      if (strpos($comment, '(dup)') !== FALSE) {
        $comment = '(duplicate) ' . str_replace('(dup)', '', $comment);
      }
      $output = "#: {$comment}\n";
      if ($build_mode == POTX_BUILD_SINGLE) {

        // File name forcing in single mode.
        $file_name = $force_name;
      }
      elseif (strpos($comment, '.info')) {

        // Store .info file strings either in general.pot or the module pot file,
        // depending on the mode used.
        $file_name = $build_mode == POTX_BUILD_CORE ? 'general' : str_replace('.info', '.module', $file_name);
      }
      elseif ($multiple_locations) {

        // Else if occured more than once, store in general.pot.
        $file_name = 'general';
      }
      else {

        // Fold multiple files in the same folder into one.
        if (empty($last_location) || $last_location == '.') {
          $file_name = 'root';
        }
        else {
          $file_name = str_replace('/', '-', $last_location);
        }
      }
      if (strpos($string, "\0") !== FALSE) {

        // Plural strings have a null byte delimited format.
        list($singular, $plural) = explode("\0", $string);
        if (!empty($context)) {
          $output .= "msgctxt \"{$context}\"\n";
        }
        $output .= "msgid \"{$singular}\"\n";
        $output .= "msgid_plural \"{$plural}\"\n";
        if (isset($translation_export_langcode)) {
          $output .= _potx_translation_export($translation_export_langcode, $singular, $plural, $api_version);
        }
        else {
          $output .= "msgstr[0] \"\"\n";
          $output .= "msgstr[1] \"\"\n";
        }
      }
      else {

        // Simple strings.
        if (!empty($context)) {
          $output .= "msgctxt \"{$context}\"\n";
        }
        $output .= "msgid \"{$string}\"\n";
        if (isset($translation_export_langcode)) {
          $output .= _potx_translation_export($translation_export_langcode, $string, NULL, $api_version);
        }
        else {
          $output .= "msgstr \"\"\n";
        }
      }
      $output .= "\n";

      // Store the generated output in the given file storage.
      if (!isset($_potx_store[$file_name])) {
        $_potx_store[$file_name] = array(
          'header' => $header_callback($file_name, $template_export_langcode, $api_version),
          'sources' => $file_list,
          'strings' => $output,
          'count' => 1,
        );
      }
      else {

        // Maintain a list of unique file names.
        $_potx_store[$file_name]['sources'] = array_unique(array_merge($_potx_store[$file_name]['sources'], $file_list));
        $_potx_store[$file_name]['strings'] .= $output;
        $_potx_store[$file_name]['count'] += 1;
      }
    }
  }
}