function 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

drupal/core/modules/locale/lib/Drupal/locale/PoDatabaseWriter.php, line 156
Definition of Drupal\locale\PoDatabaseWriter.

Class

PoDatabaseWriter
Gettext PO writer working with the locale module database.

Namespace

Drupal\locale

Code

function setHeader(PoHeader $header) {
  $this->_header = $header;
  $config = config('locale.settings');
  $locale_plurals = \Drupal::state()
    ->get('locale.translation.plurals') ?: array();

  // 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))) {
      list($nplurals, $formula) = $p;
      $locale_plurals[$langcode] = array(
        'plurals' => $nplurals,
        'formula' => $formula,
      );
      \Drupal::state()
        ->set('locale.translation.plurals', $locale_plurals);
    }
  }
}