class TranslationManager
Same name in other branches
- 9 core/lib/Drupal/Core/StringTranslation/TranslationManager.php \Drupal\Core\StringTranslation\TranslationManager
- 10 core/lib/Drupal/Core/StringTranslation/TranslationManager.php \Drupal\Core\StringTranslation\TranslationManager
- 11.x core/lib/Drupal/Core/StringTranslation/TranslationManager.php \Drupal\Core\StringTranslation\TranslationManager
Defines a chained translation implementation combining multiple translators.
Hierarchy
- class \Drupal\Core\StringTranslation\TranslationManager implements \Drupal\Core\StringTranslation\TranslationInterface, \Drupal\Core\StringTranslation\Translator\TranslatorInterface
Expanded class hierarchy of TranslationManager
4 files declare their use of TranslationManager
- ContextDefinitionIsSatisfiedTest.php in core/
tests/ Drupal/ Tests/ Core/ Plugin/ Context/ ContextDefinitionIsSatisfiedTest.php - EntityContextDefinitionIsSatisfiedTest.php in core/
tests/ Drupal/ Tests/ Core/ Plugin/ Context/ EntityContextDefinitionIsSatisfiedTest.php - HelpTopicSection.php in core/
modules/ help_topics/ src/ Plugin/ HelpSection/ HelpTopicSection.php - TranslationManagerTest.php in core/
tests/ Drupal/ Tests/ Core/ StringTranslation/ TranslationManagerTest.php - Contains \Drupal\Tests\Core\StringTranslation\TranslationManagerTest.
1 string reference to 'TranslationManager'
- core.services.yml in core/
core.services.yml - core/core.services.yml
1 service uses TranslationManager
File
-
core/
lib/ Drupal/ Core/ StringTranslation/ TranslationManager.php, line 11
Namespace
Drupal\Core\StringTranslationView source
class TranslationManager implements TranslationInterface, TranslatorInterface {
/**
* An unsorted array of arrays of active translators.
*
* An associative array. The keys are integers that indicate priority. Values
* are arrays of TranslatorInterface objects.
*
* @var \Drupal\Core\StringTranslation\Translator\TranslatorInterface[][]
*
* @see \Drupal\Core\StringTranslation\TranslationManager::addTranslator()
* @see \Drupal\Core\StringTranslation\TranslationManager::sortTranslators()
*/
protected $translators = [];
/**
* An array of translators, sorted by priority.
*
* If this is NULL a rebuild will be triggered.
*
* @var null|\Drupal\Core\StringTranslation\Translator\TranslatorInterface[]
*
* @see \Drupal\Core\StringTranslation\TranslationManager::addTranslator()
* @see \Drupal\Core\StringTranslation\TranslationManager::sortTranslators()
*/
protected $sortedTranslators = NULL;
/**
* The default langcode used in translations.
*
* @var string
* A language code.
*/
protected $defaultLangcode;
/**
* Constructs a TranslationManager object.
*
* @param \Drupal\Core\Language\LanguageDefault $default_language
* The default language.
*/
public function __construct(LanguageDefault $default_language) {
$this->defaultLangcode = $default_language->get()
->getId();
}
/**
* Appends a translation system to the translation chain.
*
* @param \Drupal\Core\StringTranslation\Translator\TranslatorInterface $translator
* The translation interface to be appended to the translation chain.
* @param int $priority
* The priority of the logger being added.
*
* @return $this
*/
public function addTranslator(TranslatorInterface $translator, $priority = 0) {
$this->translators[$priority][] = $translator;
// Reset sorted translators property to trigger rebuild.
$this->sortedTranslators = NULL;
return $this;
}
/**
* Sorts translators according to priority.
*
* @return \Drupal\Core\StringTranslation\Translator\TranslatorInterface[]
* A sorted array of translator objects.
*/
protected function sortTranslators() {
$sorted = [];
krsort($this->translators);
foreach ($this->translators as $translators) {
$sorted = array_merge($sorted, $translators);
}
return $sorted;
}
/**
* {@inheritdoc}
*/
public function getStringTranslation($langcode, $string, $context) {
if ($this->sortedTranslators === NULL) {
$this->sortedTranslators = $this->sortTranslators();
}
foreach ($this->sortedTranslators as $translator) {
$translation = $translator->getStringTranslation($langcode, $string, $context);
if ($translation !== FALSE) {
return $translation;
}
}
// No translator got a translation.
return FALSE;
}
/**
* {@inheritdoc}
*/
public function translate($string, array $args = [], array $options = []) {
return new TranslatableMarkup($string, $args, $options, $this);
}
/**
* {@inheritdoc}
*/
public function translateString(TranslatableMarkup $translated_string) {
return $this->doTranslate($translated_string->getUntranslatedString(), $translated_string->getOptions());
}
/**
* Translates a string to the current language or to a given language.
*
* @param string $string
* A string containing the English text to translate.
* @param 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 string
* The translated string.
*/
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;
}
/**
* {@inheritdoc}
*/
public function formatPlural($count, $singular, $plural, array $args = [], array $options = []) {
return new PluralTranslatableMarkup($count, $singular, $plural, $args, $options, $this);
}
/**
* Sets the default langcode.
*
* @param string $langcode
* A language code.
*/
public function setDefaultLangcode($langcode) {
$this->defaultLangcode = $langcode;
}
/**
* {@inheritdoc}
*/
public function reset() {
if ($this->sortedTranslators === NULL) {
$this->sortedTranslators = $this->sortTranslators();
}
foreach ($this->sortedTranslators as $translator) {
$translator->reset();
}
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|
TranslationManager::$defaultLangcode | protected | property | The default langcode used in translations. | ||
TranslationManager::$sortedTranslators | protected | property | An array of translators, sorted by priority. | ||
TranslationManager::$translators | protected | property | An unsorted array of arrays of active translators. | ||
TranslationManager::addTranslator | public | function | Appends a translation system to the translation chain. | ||
TranslationManager::doTranslate | protected | function | Translates a string to the current language or to a given language. | ||
TranslationManager::formatPlural | public | function | Formats a string containing a count of items. | Overrides TranslationInterface::formatPlural | |
TranslationManager::getStringTranslation | public | function | Retrieves English string to given language. | Overrides TranslatorInterface::getStringTranslation | |
TranslationManager::reset | public | function | Resets translation cache. | Overrides TranslatorInterface::reset | |
TranslationManager::setDefaultLangcode | public | function | Sets the default langcode. | ||
TranslationManager::sortTranslators | protected | function | Sorts translators according to priority. | ||
TranslationManager::translate | public | function | Translates a string to the current language or to a given language. | Overrides TranslationInterface::translate | |
TranslationManager::translateString | public | function | Translates a TranslatableMarkup object to a string. | Overrides TranslationInterface::translateString | |
TranslationManager::__construct | public | function | Constructs a TranslationManager object. | 1 |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.