interface TranslationInterface (View source)

Interface for the translation.manager translation service.

Methods

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.

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

Formats a string containing a count of items.

Details

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.

See also

FormattableMarkup::placeholderFormat
TranslatableMarkup::__construct

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.

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.

See also

TranslationInterface::translate
t()
FormattableMarkup
PluralTranslatableMarkup::createFromTranslatedString