public function TranslationManager::translate

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

Parameters

string $string: A string containing the English string to translate.

array $args: 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\Core\Utility\String::format() for details.

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.

See also

t()

\Drupal\Core\Utility\String::format()

File

drupal/core/lib/Drupal/Core/StringTranslation/TranslationManager.php, line 128
Contains \Drupal\Core\StringTranslation\TranslationManager.

Class

TranslationManager
Defines a chained translation implementation combining multiple translators.

Namespace

Drupal\Core\StringTranslation

Code

public function translate($string, array $args = array(), array $options = array()) {

  // Merge in defaults.
  if (empty($options['langcode'])) {
    $options['langcode'] = $this->defaultLangcode;
  }
  if (empty($options['context'])) {
    $options['context'] = '';
  }
  $translation = $this
    ->getStringTranslation($options['langcode'], $string, $options['context']);
  $string = $translation === FALSE ? $string : $translation;
  if (empty($args)) {
    return $string;
  }
  else {
    return String::format($string, $args);
  }
}