You are here

function _potx_get_header in Translation template extractor 8

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

Returns a header generated for a given file.

Parameters

string $file: Name of POT file to generate the header for.

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

int $api_version: Drupal API version to work with.

Return value

string The header for the translation file.

6 string references to '_potx_get_header'
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 711
Extraction API used by the web and command line interface.

Code

function _potx_get_header($file, $template_export_langcode = NULL, $api_version = POTX_API_CURRENT) {

  // We only have language to use if we should export with that langcode.
  $language = NULL;
  if (isset($template_export_langcode)) {
    if ($api_version >= POTX_API_8) {
      $languages = \Drupal::languageManager()
        ->getLanguages();

      /** @var \Drupal\locale\PluralFormula $PluralFormula */
      $plural_formula = \Drupal::service('locale.plural.formula');
      $language = (object) [
        'name' => $languages[$template_export_langcode]
          ->getName(),
        'plurals' => $plural_formula
          ->getNumberOfPlurals($template_export_langcode),
        // @todo PluralFormulaInterface->getFormula() returns a plural element
        // stack instead of an arithmetic formula. This will be fixed in
        // https://www.drupal.org/node/2882617
        // 'formula' => $plural_formula->getFormula($template_export_langcode),
        'formula' => _potx_get_plural_form($template_export_langcode),
      ];
    }
    else {
      $language = \Drupal::database()
        ->query($api_version > POTX_API_5 ? "SELECT language, name, plurals, formula FROM {languages} WHERE language = :langcode" : "SELECT locale, name, plurals, formula FROM {locales_meta} WHERE locale = :langcode", [
        ':langcode' => $template_export_langcode,
      ])
        ->fetchObject();
    }
  }
  $output = '# $Id$' . "\n";
  $output .= "#\n";
  $output .= '# ' . (isset($language) ? $language->name : 'LANGUAGE') . ' translation of Drupal (' . $file . ")\n";
  $output .= "# Copyright YEAR NAME <EMAIL@ADDRESS>\n";
  $output .= "# --VERSIONS--\n";
  $output .= "#\n";
  $output .= "#, fuzzy\n";
  $output .= "msgid \"\"\n";
  $output .= "msgstr \"\"\n";
  $output .= "\"Project-Id-Version: PROJECT VERSION\\n\"\n";
  $output .= '"POT-Creation-Date: ' . date("Y-m-d H:iO") . "\\n\"\n";
  $output .= '"PO-Revision-Date: ' . (isset($language) ? date("Y-m-d H:iO") : 'YYYY-mm-DD HH:MM+ZZZZ') . "\\n\"\n";
  $output .= "\"Last-Translator: NAME <EMAIL@ADDRESS>\\n\"\n";
  $output .= "\"Language-Team: " . (isset($language) ? $language->name : 'LANGUAGE') . " <EMAIL@ADDRESS>\\n\"\n";
  $output .= "\"MIME-Version: 1.0\\n\"\n";
  $output .= "\"Content-Type: text/plain; charset=utf-8\\n\"\n";
  $output .= "\"Content-Transfer-Encoding: 8bit\\n\"\n";
  if (isset($language->formula) && isset($language->plurals)) {
    $output .= "\"Plural-Forms: nplurals=" . $language->plurals . "; plural=" . strtr($language->formula, [
      '$' => '',
    ]) . ";\\n\"\n\n";
  }
  else {
    $output .= "\"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\\n\"\n\n";
  }
  return $output;
}