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