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

__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.

int
evaluatePlural(array $element_stack, int $n)

Evaluate the plural element stack using a plural value.

Details

__construct(string $langcode = NULL)

Constructor, creates a PoHeader with default values.

Parameters

string $langcode

Language code.

string getPluralForms()

Gets the plural form.

Return Value

string

Plural form component from the header, for example: 'nplurals=2; plural=(n > 1);'.

setLanguageName(string $languageName)

Set the human readable language name.

Parameters

string $languageName

Human readable language name.

string getLanguageName()

Gets the human readable language name.

Return Value

string

The human readable language name.

setProjectName(string $projectName)

Set the project name.

Parameters

string $projectName

Human readable project name.

string getProjectName()

Gets the project name.

Return Value

string

The human readable project name.

setFromString(string $header)

Populate internal values from a string.

Parameters

string $header

Full header string with key-value pairs.

__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.

Parameters

string $pluralforms

The Plural-Forms entry value.

Return Value

An

indexed array of parsed plural formula data. Containing:

  • 'nplurals': The number of plural forms defined by the plural formula.
  • 'plurals': Array of plural positions keyed by plural value.

Exceptions

Exception

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.

Parameters

array $element_stack

Array of plural formula values and operators create by parseArithmetic().

int $n

The @count number for which we are determining the right plural position.

Return Value

int

Number of the plural string to be used for the given plural value.

Exceptions

Exception

See also

parseArithmetic()