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)

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

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

static int extractPluralCount(string $expr)

Extracts number of plurals from plurals form expression.

Parameters

string $expr Expression to process

Return Value

int

static string extractPluralsForms(string $header)

Parse full PO header and extract only plural forms line.

Parameters

string $header Gettext header

Return Value

string

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

string pgettext(string $msgctxt, string $msgid)

Translate with context.

Parameters

string $msgctxt Context
string $msgid String to be translated

Return Value

string

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

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