function PoDatabaseWriter::setHeader

Same name in other branches
  1. 9 core/modules/locale/src/PoDatabaseWriter.php \Drupal\locale\PoDatabaseWriter::setHeader()
  2. 8.9.x core/modules/locale/src/PoDatabaseWriter.php \Drupal\locale\PoDatabaseWriter::setHeader()
  3. 10 core/modules/locale/src/PoDatabaseWriter.php \Drupal\locale\PoDatabaseWriter::setHeader()

Implements Drupal\Component\Gettext\PoMetadataInterface::setHeader().

Sets the header and configure Drupal accordingly.

Before being able to process the given header we need to know in what context this database write is done. For this the options must be set.

A langcode is required to set the current header's PluralForm.

Parameters

\Drupal\Component\Gettext\PoHeader $header: Header metadata.

Throws

\Exception

Overrides PoMetadataInterface::setHeader

File

core/modules/locale/src/PoDatabaseWriter.php, line 159

Class

PoDatabaseWriter
Gettext PO writer working with the locale module database.

Namespace

Drupal\locale

Code

public function setHeader(PoHeader $header) {
    $this->header = $header;
    $locale_plurals = \Drupal::state()->get('locale.translation.plurals', []);
    // Check for options.
    $options = $this->getOptions();
    if (empty($options)) {
        throw new \Exception('Options should be set before assigning a PoHeader.');
    }
    $overwrite_options = $options['overwrite_options'];
    // Check for langcode.
    $langcode = $this->langcode;
    if (empty($langcode)) {
        throw new \Exception('Langcode should be set before assigning a PoHeader.');
    }
    if (array_sum($overwrite_options) || empty($locale_plurals[$langcode]['plurals'])) {
        // Get and store the plural formula if available.
        $plural = $header->getPluralForms();
        if (isset($plural) && ($p = $header->parsePluralForms($plural))) {
            [
                $nplurals,
                $formula,
            ] = $p;
            \Drupal::service('locale.plural.formula')->setPluralFormula($langcode, $nplurals, $formula);
        }
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.