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(CacheInterface|string|null $cache)

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(CacheInterface|string|null $cache)

No description

Parameters

CacheInterface|string|null $cache

Mo file to load (null for no file) or a CacheInterface implementation

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