class Translator (View source)

Provides a simple gettext replacement that works independently from the system's gettext abilities.

It can read MO files and use them for translating strings.

It caches ll strings and translations to speed up the string lookup.

Constants

ERROR_NONE

None error.

ERROR_DOES_NOT_EXIST

File does not exist.

ERROR_BAD_MAGIC

File has bad magic number.

ERROR_READING

Error while reading file, probably too short.

MAGIC_BE

Big endian mo file magic bytes.

MAGIC_LE

Little endian mo file magic bytes.

Properties

int $error

Parse error code (0 if no error).

Methods

__construct(string|null $filename)

No description

string
gettext(string $msgid)

Translates a string.

bool
exists(string $msgid)

Check if a string is translated.

static string
sanitizePluralExpression(string $expr)

Sanitize plural form expression for use in ExpressionLanguage.

static int
extractPluralCount(string $expr)

Extracts number of plurals from plurals form expression.

static string
extractPluralsForms(string $header)

Parse full PO header and extract only plural forms line.

string
ngettext(string $msgid, string $msgidPlural, int $number)

Plural version of gettext.

string
pgettext(string $msgctxt, string $msgid)

Translate with context.

string
npgettext(string $msgctxt, string $msgid, string $msgidPlural, int $number)

Plural version of pgettext.

void
setTranslation(string $msgid, string $msgstr)

Set translation in place

void
setTranslations(array $translations)

Set the translations

array
getTranslations()

Get the translations

Details

__construct(string|null $filename)

No description

Parameters

string|null $filename

Name of mo file to load (null to not load a file)

string gettext(string $msgid)

Translates a string.

Parameters

string $msgid

String to be translated

Return Value

string

translated string (or original, if not found)

bool exists(string $msgid)

Check if a string is translated.

Parameters

string $msgid

String to be checked

Return Value

bool

static string sanitizePluralExpression(string $expr)

Sanitize plural form expression for use in ExpressionLanguage.

Parameters

string $expr

Expression to sanitize

Return Value

string

sanitized plural form expression

static int extractPluralCount(string $expr)

Extracts number of plurals from plurals form expression.

Parameters

string $expr

Expression to process

Return Value

int

Total number of plurals

static string extractPluralsForms(string $header)

Parse full PO header and extract only plural forms line.

Parameters

string $header

Gettext header

Return Value

string

verbatim plural form header field

string ngettext(string $msgid, string $msgidPlural, int $number)

Plural version of gettext.

Parameters

string $msgid

Single form

string $msgidPlural

Plural form

int $number

Number of objects

Return Value

string

translated plural form

string pgettext(string $msgctxt, string $msgid)

Translate with context.

Parameters

string $msgctxt

Context

string $msgid

String to be translated

Return Value

string

translated plural form

string npgettext(string $msgctxt, string $msgid, string $msgidPlural, int $number)

Plural version of pgettext.

Parameters

string $msgctxt

Context

string $msgid

Single form

string $msgidPlural

Plural form

int $number

Number of objects

Return Value

string

translated plural form

void setTranslation(string $msgid, string $msgstr)

Set translation in place

Parameters

string $msgid

String to be set

string $msgstr

Translation

Return Value

void

void setTranslations(array $translations)

Set the translations

Parameters

array $translations

The translations "key => value" array

Return Value

void

array getTranslations()

Get the translations

Return Value

array

The translations "key => value" array