class TranslationManager implements TranslationInterface, TranslatorInterface (View source)

Defines a chained translation implementation combining multiple translators.

Properties

protected TranslatorInterface[][] $translators

An unsorted array of arrays of active translators.

protected null|TranslatorInterface[] $sortedTranslators

An array of translators, sorted by priority.

protected string $defaultLangcode

The default langcode used in translations.

Methods

__construct(LanguageDefault $default_language)

Constructs a TranslationManager object.

$this
addTranslator(TranslatorInterface $translator, int $priority = 0)

Appends a translation system to the translation chain.

sortTranslators()

Sorts translators according to priority.

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

Retrieves English string to given language.

translate(string $string, array $args = [], array $options = [])

Translates a string to the current language or to a given language.

string
translateString(TranslatableMarkup $translated_string)

Translates a TranslatableMarkup object to a string.

string
doTranslate(string $string, array $options = [])

Translates a string to the current language or to a given language.

formatPlural(int $count, string $singular, string $plural, array $args = [], array $options = [])

Formats a string containing a count of items.

setDefaultLangcode(string $langcode)

Sets the default langcode.

reset()

Resets translation cache.

Details

__construct(LanguageDefault $default_language)

Constructs a TranslationManager object.

Parameters

LanguageDefault $default_language

The default language.

$this addTranslator(TranslatorInterface $translator, int $priority = 0)

Appends a translation system to the translation chain.

Parameters

TranslatorInterface $translator

The translation interface to be appended to the translation chain.

int $priority

The priority of the logger being added.

Return Value

$this

protected TranslatorInterface[] sortTranslators()

Sorts translators according to priority.

Return Value

TranslatorInterface[]

A sorted array of translator objects.

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.

TranslatableMarkup translate(string $string, array $args = [], array $options = [])

Translates a string to the current language or to a given language.

Never call this translate() method directly. In order for strings to be localized, make them available in one of the ways supported by the

Parameters

string $string

A string containing the English text to translate.

array $args

(optional) An associative array of replacements to make after translation. Based on the first character of the key, the value is escaped and/or themed. See \Drupal\Component\Render\FormattableMarkup::placeholderFormat() for details.

array $options

(optional) An associative array of additional options, with the following elements:

  • 'langcode' (defaults to the current language): A language code, to translate to a language other than what is used to display the page.
  • 'context' (defaults to the empty context): The context the source string belongs to. See the @link i18n Internationalization topic @endlink for more information about string contexts.

Return Value

TranslatableMarkup

An object that, when cast to a string, returns the translated string.

string translateString(TranslatableMarkup $translated_string)

Translates a TranslatableMarkup object to a string.

Parameters

TranslatableMarkup $translated_string

A TranslatableMarkup object.

Return Value

string

The translated string.

protected string doTranslate(string $string, array $options = [])

Translates a string to the current language or to a given language.

Parameters

string $string

A string containing the English text to translate.

array $options

An associative array of additional options, with the following elements:

  • 'langcode': The language code to translate to a language other than what is used to display the page.
  • 'context': The context the source string belongs to.

Return Value

string

The translated string.

PluralTranslatableMarkup formatPlural(int $count, string $singular, string $plural, array $args = [], array $options = [])

Formats a string containing a count of items.

This function ensures that the string is pluralized correctly. Since TranslationInterface::translate() is called by this function, make sure not to pass already-localized strings to it. See PluralTranslatableMarkup::createFromTranslatedString() for that.

For example:

Parameters

int $count

The item count to display.

string $singular

The string for the singular case. Make sure it is clear this is singular, to ease translation (e.g. use "1 new comment" instead of "1 new"). Do not use @count in the singular string.

string $plural

The string for the plural case. Make sure it is clear this is plural, to ease translation. Use @count in place of the item count, as in "@count new comments".

array $args

An associative array of replacements to make after translation. Instances of any key in this array are replaced with the corresponding value. Based on the first character of the key, the value is escaped and/or themed. See \Drupal\Component\Render\FormattableMarkup. Note that you do not need to include @count in this array; this replacement is done automatically for the plural cases.

array $options

An associative array of additional options. See t() for allowed keys.

Return Value

PluralTranslatableMarkup

A translated string.

setDefaultLangcode(string $langcode)

Sets the default langcode.

Parameters

string $langcode

A language code.

reset()

Resets translation cache.

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