class Translator

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 $filename)

Constructor.

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

Details

at line 96
__construct(string $filename)

Constructor.

Parameters

string $filename Name of mo file to load

at line 147
string gettext(string $msgid)

Translates a string.

Parameters

string $msgid String to be translated

Return Value

string translated string (or original, if not found)

at line 163
bool exists(string $msgid)

Check if a string is translated.

Parameters

string $msgid String to be checked

Return Value

bool

at line 175
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

at line 207
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

at line 228
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

at line 302
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

at line 331
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

at line 352
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

at line 371
void setTranslation(string $msgid, string $msgstr)

Set translation in place

Parameters

string $msgid String to be set
string $msgstr Translation

Return Value

void