PoHeader
class PoHeader (View source)
Gettext PO header handler.
Possible Gettext PO header elements are explained in http://www.gnu.org/software/gettext/manual/gettext.html#Header-Entry, but we only support a subset of these directly.
Example header:
"Project-Id-Version: Drupal core (7.11)\n" "POT-Creation-Date: 2012-02-12 22:59+0000\n" "PO-Revision-Date: YYYY-mm-DD HH:MM+ZZZZ\n" "Language-Team: Catalan\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=utf-8\n" "Content-Transfer-Encoding: 8bit\n" "Plural-Forms: nplurals=2; plural=(n>1);\n"
Properties
| protected string | $langcode | Language code. |
|
| protected string | $pluralForms | Formula for the plural form. |
|
| protected string | $authors | Author(s) of the file. |
|
| protected string | $poDate | Date the po file got created. |
|
| protected string | $languageName | Human readable language name. |
|
| protected string | $projectName | Name of the project the translation belongs to. |
Methods
Constructor, creates a PoHeader with default values.
Gets the plural form.
Set the human readable language name.
Gets the human readable language name.
Set the project name.
Gets the project name.
Populate internal values from a string.
Generate a Gettext PO formatted header string based on data set earlier.
Parses a Plural-Forms entry from a Gettext Portable Object file header.
Evaluate the plural element stack using a plural value.
Details
__construct(string $langcode = NULL)
Constructor, creates a PoHeader with default values.
string
getPluralForms()
Gets the plural form.
setLanguageName(string $languageName)
Set the human readable language name.
string
getLanguageName()
Gets the human readable language name.
setProjectName(string $projectName)
Set the project name.
string
getProjectName()
Gets the project name.
setFromString(string $header)
Populate internal values from a string.
__toString()
Generate a Gettext PO formatted header string based on data set earlier.
An
parsePluralForms(string $pluralforms)
Parses a Plural-Forms entry from a Gettext Portable Object file header.
protected int
evaluatePlural(array $element_stack, int $n)
Evaluate the plural element stack using a plural value.
Using an element stack, which represents a plural formula, we calculate which plural string should be used for a given plural value.
An example of plural formula parting and evaluation: Plural formula: 'n!=1' This formula is parsed by parseArithmetic() to a stack (array) of elements: array( 0 => '$n', 1 => '1', 2 => '!=', ); The evaluatePlural() method evaluates the $element_stack using the plural value $n. Before the actual evaluation, the '$n' in the array is replaced by the value of $n. For example: $n = 2 results in: array( 0 => '2', 1 => '1', 2 => '!=', ); The stack is processed until only one element is (the result) is left. In every iteration the top elements of the stack, up until the first operator, are evaluated. After evaluation the arguments and the operator itself are removed and replaced by the evaluation result. This is typically 2 arguments and 1 element for the operator. Because the operator is '!=' the example stack is evaluated as: $f = (int) 2 != 1; The resulting stack is: array( 0 => 1, ); With only one element left in the stack (the final result) the loop is terminated and the result is returned.