You are here

function _potx_build_files in Translation template extractor 5

Same name and namespace in other branches
  1. 8 potx.inc \_potx_build_files()
  2. 5.2 potx.inc \_potx_build_files()
  3. 6.3 potx.inc \_potx_build_files()
  4. 6 potx.inc \_potx_build_files()
  5. 6.2 potx.inc \_potx_build_files()
  6. 7.3 potx.inc \_potx_build_files()
  7. 7 potx.inc \_potx_build_files()
  8. 7.2 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.

2 calls to _potx_build_files()
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 239
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_6) {
  global $_potx_store;

  // Get strings and versions by reference.
  $strings = $save_callback(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 => $file_info) {

    // Build a compact list of files this string occured in.
    $occured = $file_list = array();
    foreach ($file_info as $file => $lines) {
      $occured[] = "{$file}:" . join(';', $lines);
      if (isset($versions[$file])) {
        $file_list[] = $versions[$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";

    // File name forcing in single mode.
    if ($build_mode == POTX_BUILD_SINGLE) {
      $file_name = $force_name;
    }
    elseif (strpos($comment, '.info')) {
      $file_name = $build_mode == POTX_BUILD_CORE ? 'general' : str_replace('.info', '.module', $file_name);
    }
    else {
      $file_name = count($occured) > 1 ? 'general' : $file;
    }
    if (strpos($string, "\0") !== FALSE) {

      // Plural strings have a null byte delimited format.
      list($singular, $plural) = explode("\0", $string);
      $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.
      $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, $translation_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;
    }
  }
}