Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. PoDatabaseWriter.php

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

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

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal