class StaticTranslation implements TranslatorInterface (View source)

String translator with a static cache for translations.

This is a high performance way to provide a handful of string replacements.

Properties

protected array $translations

String translations.

Methods

__construct(array $translations = [])

Constructs a translator from an array of translations.

string|false
getStringTranslation(string $langcode, string $string, string $context)

Retrieves English string to given language.

reset()

Resets translation cache.

array
getLanguage(string $langcode)

Retrieves translations for a given language.

Details

__construct(array $translations = [])

Constructs a translator from an array of translations.

Parameters

array $translations

Array of override strings indexed by language and context

string|false getStringTranslation(string $langcode, string $string, string $context)

Retrieves English string to given language.

Parameters

string $langcode

Language code to translate to.

string $string

The source string.

string $context

The string context.

Return Value

string|false

Translated string if there is a translation, FALSE if not.

reset()

Resets translation cache.

Since most translation systems implement some form of caching, this provides a way to delete that cache.

protected array getLanguage(string $langcode)

Retrieves translations for a given language.

Parameters

string $langcode

The langcode of the language.

Return Value

array

A multidimensional array of translations, indexed by the context the source string belongs to. The second level is using original strings as keys. An empty array will be returned when no translations are available.