You are here

public function PoDatabaseWriter::setHeader in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/locale/src/PoDatabaseWriter.php \Drupal\locale\PoDatabaseWriter::setHeader()
  2. 9 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

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);
    }
  }
}