function PoHeader::parsePluralForms

Parses a Plural-Forms entry from a Gettext Portable Object file header.

Parameters

string $pluralforms: The Plural-Forms entry value.

Return value

An array containing the number of plural forms and the converted version of the formula that can be evaluated with PHP later.

File

drupal/core/lib/Drupal/Component/Gettext/PoHeader.php, line 193
Definition of Drupal\Component\Gettext\PoHeader

Class

PoHeader
Gettext PO header handler.

Namespace

Drupal\Component\Gettext

Code

function parsePluralForms($pluralforms) {

  // First, delete all whitespace.
  $pluralforms = strtr($pluralforms, array(
    " " => "",
    "\t" => "",
  ));

  // Select the parts that define nplurals and plural.
  $nplurals = strstr($pluralforms, "nplurals=");
  if (strpos($nplurals, ";")) {

    // We want the string from the 10th char, because "nplurals=" length is 9.
    $nplurals = substr($nplurals, 9, strpos($nplurals, ";") - 9);
  }
  else {
    return FALSE;
  }
  $plural = strstr($pluralforms, "plural=");
  if (strpos($plural, ";")) {

    // We want the string from the 8th char, because "plural=" length is 7.
    $plural = substr($plural, 7, strpos($plural, ";") - 7);
  }
  else {
    return FALSE;
  }

  // Get PHP version of the plural formula.
  $plural = $this
    ->parseArithmetic($plural);
  if ($plural !== FALSE) {
    return array(
      $nplurals,
      $plural,
    );
  }
  else {
    throw new Exception('The plural formula could not be parsed.');
  }
}