You are here

public function PoHeader::parsePluralForms in Localization update 7.2

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

Parameters

string $pluralforms: The Plural-Forms entry value.

Return value

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

Throws

\Exception Throws exception in case plural formula could not be parsed.

File

includes/gettext/PoHeader.php, line 196
Definition of Drupal\Component\Gettext\PoHeader.

Class

PoHeader
Gettext PO header handler.

Code

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