You are here

function _potx_build_files in Translation template extractor 8

Same name and namespace in other branches
  1. 5.2 potx.inc \_potx_build_files()
  2. 5 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

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

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

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

string $save_callback: Callback used to save strings previously.

string $version_callback: Callback used to save versions previously.

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

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

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

int $api_version: Drupal API version to work with.

6 calls to _potx_build_files()
PotxCommands::potx in src/Commands/PotxCommands.php
Extract translatable strings from Drupal source code.
PotxExtractTranslationForm::submitForm in src/Form/PotxExtractTranslationForm.php
Form submission handler.
PotxTest::buildOutput in tests/src/Kernel/PotxTest.php
Build the output from parsed files.
PotxTest::parseFile in tests/src/Kernel/PotxTest.php
Parse the given file with the given API version.
PotxTest::parsePhpContent in tests/src/Kernel/PotxTest.php
Parse the given file with the given API version.

... See full list

File

./potx.inc, line 444
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 = [];

      // 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}:" . implode(';', $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 = implode(" ", $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] = [
          '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;
      }
    }
  }
}