TranslationManager.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. namespace Drupal\Core\StringTranslation;
  3. use Drupal\Core\Language\LanguageDefault;
  4. use Drupal\Core\StringTranslation\Translator\TranslatorInterface;
  5. /**
  6. * Defines a chained translation implementation combining multiple translators.
  7. */
  8. class TranslationManager implements TranslationInterface, TranslatorInterface {
  9. /**
  10. * An unsorted array of arrays of active translators.
  11. *
  12. * An associative array. The keys are integers that indicate priority. Values
  13. * are arrays of TranslatorInterface objects.
  14. *
  15. * @var \Drupal\Core\StringTranslation\Translator\TranslatorInterface[][]
  16. *
  17. * @see \Drupal\Core\StringTranslation\TranslationManager::addTranslator()
  18. * @see \Drupal\Core\StringTranslation\TranslationManager::sortTranslators()
  19. */
  20. protected $translators = [];
  21. /**
  22. * An array of translators, sorted by priority.
  23. *
  24. * If this is NULL a rebuild will be triggered.
  25. *
  26. * @var null|\Drupal\Core\StringTranslation\Translator\TranslatorInterface[]
  27. *
  28. * @see \Drupal\Core\StringTranslation\TranslationManager::addTranslator()
  29. * @see \Drupal\Core\StringTranslation\TranslationManager::sortTranslators()
  30. */
  31. protected $sortedTranslators = NULL;
  32. /**
  33. * The default langcode used in translations.
  34. *
  35. * @var string
  36. * A language code.
  37. */
  38. protected $defaultLangcode;
  39. /**
  40. * Constructs a TranslationManager object.
  41. *
  42. * @param \Drupal\Core\Language\LanguageDefault $default_language
  43. * The default language.
  44. */
  45. public function __construct(LanguageDefault $default_language) {
  46. $this->defaultLangcode = $default_language->get()->getId();
  47. }
  48. /**
  49. * Appends a translation system to the translation chain.
  50. *
  51. * @param \Drupal\Core\StringTranslation\Translator\TranslatorInterface $translator
  52. * The translation interface to be appended to the translation chain.
  53. * @param int $priority
  54. * The priority of the logger being added.
  55. *
  56. * @return $this
  57. */
  58. public function addTranslator(TranslatorInterface $translator, $priority = 0) {
  59. $this->translators[$priority][] = $translator;
  60. // Reset sorted translators property to trigger rebuild.
  61. $this->sortedTranslators = NULL;
  62. return $this;
  63. }
  64. /**
  65. * Sorts translators according to priority.
  66. *
  67. * @return \Drupal\Core\StringTranslation\Translator\TranslatorInterface[]
  68. * A sorted array of translator objects.
  69. */
  70. protected function sortTranslators() {
  71. $sorted = [];
  72. krsort($this->translators);
  73. foreach ($this->translators as $translators) {
  74. $sorted = array_merge($sorted, $translators);
  75. }
  76. return $sorted;
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. public function getStringTranslation($langcode, $string, $context) {
  82. if ($this->sortedTranslators === NULL) {
  83. $this->sortedTranslators = $this->sortTranslators();
  84. }
  85. foreach ($this->sortedTranslators as $translator) {
  86. $translation = $translator->getStringTranslation($langcode, $string, $context);
  87. if ($translation !== FALSE) {
  88. return $translation;
  89. }
  90. }
  91. // No translator got a translation.
  92. return FALSE;
  93. }
  94. /**
  95. * {@inheritdoc}
  96. */
  97. public function translate($string, array $args = [], array $options = []) {
  98. return new TranslatableMarkup($string, $args, $options, $this);
  99. }
  100. /**
  101. * {@inheritdoc}
  102. */
  103. public function translateString(TranslatableMarkup $translated_string) {
  104. return $this->doTranslate($translated_string->getUntranslatedString(), $translated_string->getOptions());
  105. }
  106. /**
  107. * Translates a string to the current language or to a given language.
  108. *
  109. * @param string $string
  110. * A string containing the English text to translate.
  111. * @param array $options
  112. * An associative array of additional options, with the following elements:
  113. * - 'langcode': The language code to translate to a language other than
  114. * what is used to display the page.
  115. * - 'context': The context the source string belongs to.
  116. *
  117. * @return string
  118. * The translated string.
  119. */
  120. protected function doTranslate($string, array $options = []) {
  121. // If a NULL langcode has been provided, unset it.
  122. if (!isset($options['langcode']) && array_key_exists('langcode', $options)) {
  123. unset($options['langcode']);
  124. }
  125. // Merge in options defaults.
  126. $options = $options + [
  127. 'langcode' => $this->defaultLangcode,
  128. 'context' => '',
  129. ];
  130. $translation = $this->getStringTranslation($options['langcode'], $string, $options['context']);
  131. return $translation === FALSE ? $string : $translation;
  132. }
  133. /**
  134. * {@inheritdoc}
  135. */
  136. public function formatPlural($count, $singular, $plural, array $args = [], array $options = []) {
  137. return new PluralTranslatableMarkup($count, $singular, $plural, $args, $options, $this);
  138. }
  139. /**
  140. * Sets the default langcode.
  141. *
  142. * @param string $langcode
  143. * A language code.
  144. */
  145. public function setDefaultLangcode($langcode) {
  146. $this->defaultLangcode = $langcode;
  147. }
  148. /**
  149. * {@inheritdoc}
  150. */
  151. public function reset() {
  152. if ($this->sortedTranslators === NULL) {
  153. $this->sortedTranslators = $this->sortTranslators();
  154. }
  155. foreach ($this->sortedTranslators as $translator) {
  156. $translator->reset();
  157. }
  158. }
  159. }