You are here

public function PoDatabaseWriter::setHeader in Localization update 7.2

Implements 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

PoHeader $header: Header metadata.

Throws

Exception Exception is thrown when required properties are not set.

Overrides PoMetadataInterface::setHeader

File

includes/locale/PoDatabaseWriter.php, line 156
Definition of PoDatabaseWriter.

Class

PoDatabaseWriter
Gettext PO writer working with the locale module database.

Code

public function setHeader(PoHeader $header) {
  $this->_header = $header;
  $languages = language_list();

  // 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.');
  }

  // Check is language is already created.
  if (!isset($languages[$langcode])) {
    throw new \Exception('Language should be known before using it.');
  }
  if (array_sum($overwrite_options) || empty($languages[$langcode]->plurals)) {

    // Get and store the plural formula if available.
    $plural = $header
      ->getPluralForms();
    if (isset($plural) && ($p = $header
      ->parsePluralForms($plural))) {
      list($nplurals, $formula) = $p;
      db_update('languages')
        ->fields(array(
        'plurals' => $nplurals,
        'formula' => $formula,
      ))
        ->condition('language', $langcode)
        ->execute();
    }
  }
}