function TranslationManager::doTranslate

Same name in other branches
  1. 9 core/lib/Drupal/Core/StringTranslation/TranslationManager.php \Drupal\Core\StringTranslation\TranslationManager::doTranslate()
  2. 8.9.x core/lib/Drupal/Core/StringTranslation/TranslationManager.php \Drupal\Core\StringTranslation\TranslationManager::doTranslate()
  3. 11.x core/lib/Drupal/Core/StringTranslation/TranslationManager.php \Drupal\Core\StringTranslation\TranslationManager::doTranslate()

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.

1 call to TranslationManager::doTranslate()
TranslationManager::translateString in core/lib/Drupal/Core/StringTranslation/TranslationManager.php

File

core/lib/Drupal/Core/StringTranslation/TranslationManager.php, line 129

Class

TranslationManager
Defines a chained translation implementation combining multiple translators.

Namespace

Drupal\Core\StringTranslation

Code

protected function doTranslate($string, array $options = []) {
    // If a NULL langcode has been provided, unset it.
    if (!isset($options['langcode']) && array_key_exists('langcode', $options)) {
        unset($options['langcode']);
    }
    // Merge in options defaults.
    $options = $options + [
        'langcode' => $this->defaultLangcode,
        'context' => '',
    ];
    $translation = $this->getStringTranslation($options['langcode'], $string, $options['context']);
    return $translation === FALSE ? $string : $translation;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.