[FEATURE] EXT:form - integrate new form framework
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / Service / TranslationService.php
1 <?php
2 declare(strict_types=1);
3 namespace TYPO3\CMS\Form\Service;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use TYPO3\CMS\Core\Localization\Locales;
19 use TYPO3\CMS\Core\Localization\LocalizationFactory;
20 use TYPO3\CMS\Core\SingletonInterface;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22 use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
23 use TYPO3\CMS\Extbase\Object\ObjectManager;
24 use TYPO3\CMS\Extbase\Utility\ArrayUtility;
25 use TYPO3\CMS\Form\Domain\Model\FormElements\FormElementInterface;
26 use TYPO3\CMS\Form\Domain\Model\Renderable\RootRenderableInterface;
27 use TYPO3\CMS\Form\Domain\Runtime\FormRuntime;
28 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
29 use TYPO3\CMS\Lang\LanguageService;
30
31 /**
32 * Advanced translations
33 * This class is subjected to change.
34 * **Do NOT subclass**
35 *
36 * Scope: frontend / backend
37 * @internal
38 */
39 class TranslationService implements SingletonInterface
40 {
41
42 /**
43 * Local Language content
44 *
45 * @var array
46 */
47 protected $LOCAL_LANG = [];
48
49 /**
50 * Contains those LL keys, which have been set to (empty) in TypoScript.
51 * This is necessary, as we cannot distinguish between a nonexisting
52 * translation and a label that has been cleared by TS.
53 * In both cases ['key'][0]['target'] is "".
54 *
55 * @var array
56 */
57 protected $LOCAL_LANG_UNSET = [];
58
59 /**
60 * Key of the language to use
61 *
62 * @var string
63 */
64 protected $languageKey = null;
65
66 /**
67 * Pointer to alternative fall-back language to use
68 *
69 * @var array
70 */
71 protected $alternativeLanguageKeys = [];
72
73 /**
74 * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
75 */
76 protected $configurationManager = null;
77
78 /**
79 * Return TranslationService as singleton
80 *
81 * @return TranslationService
82 * @internal
83 */
84 public static function getInstance()
85 {
86 return GeneralUtility::makeInstance(ObjectManager::class)->get(self::class);
87 }
88
89 /**
90 * Returns the localized label of the LOCAL_LANG key, $key.
91 *
92 * @param mixed $key The key from the LOCAL_LANG array for which to return the value.
93 * @param array $arguments the arguments of the extension, being passed over to vsprintf
94 * @param string $locallangPathAndFilename
95 * @param string $language
96 * @param mixed $defaultValue
97 * @return mixed The value from LOCAL_LANG or $defaultValue if no translation was found.
98 * @internal
99 */
100 public function translate(
101 $key,
102 array $arguments = null,
103 string $locallangPathAndFilename = null,
104 string $language = null,
105 $defaultValue = ''
106 ) {
107 $value = null;
108 $key = (string)$key;
109
110 if ($locallangPathAndFilename) {
111 $key = $locallangPathAndFilename . ':' . $key;
112 }
113
114 $keyParts = explode(':', $key);
115 if (GeneralUtility::isFirstPartOfStr($key, 'LLL:')) {
116 $locallangPathAndFilename = $keyParts[1] . ':' . $keyParts[2];
117 $key = $keyParts[3];
118 } elseif (GeneralUtility::isFirstPartOfStr($key, 'EXT:')) {
119 $locallangPathAndFilename = $keyParts[0] . ':' . $keyParts[1];
120 $key = $keyParts[2];
121 } else {
122 if (count($keyParts) === 2) {
123 $locallangPathAndFilename = $keyParts[0];
124 $key = $keyParts[1];
125 }
126 }
127
128 if ($language) {
129 $this->languageKey = $language;
130 }
131
132 $this->initializeLocalization($locallangPathAndFilename);
133
134 // The "from" charset of csConv() is only set for strings from TypoScript via _LOCAL_LANG
135 if (!empty($this->LOCAL_LANG[$this->languageKey][$key][0]['target'])
136 || isset($this->LOCAL_LANG_UNSET[$this->languageKey][$key])
137 ) {
138 // Local language translation for key exists
139 $value = $this->LOCAL_LANG[$this->languageKey][$key][0]['target'];
140 } elseif (!empty($this->alternativeLanguageKeys)) {
141 $languages = array_reverse($this->alternativeLanguageKeys);
142 foreach ($languages as $language) {
143 if (!empty($this->LOCAL_LANG[$language][$key][0]['target'])
144 || isset($this->LOCAL_LANG_UNSET[$language][$key])
145 ) {
146 // Alternative language translation for key exists
147 $value = $this->LOCAL_LANG[$language][$key][0]['target'];
148 break;
149 }
150 }
151 }
152
153 if ($value === null && (!empty($this->LOCAL_LANG['default'][$key][0]['target'])
154 || isset($this->LOCAL_LANG_UNSET['default'][$key]))
155 ) {
156 // Default language translation for key exists
157 // No charset conversion because default is English and thereby ASCII
158 $value = $this->LOCAL_LANG['default'][$key][0]['target'];
159 }
160
161 if (is_array($arguments) && $value !== null) {
162 $value = vsprintf($value, $arguments);
163 } else {
164 if (empty($value)) {
165 $value = $defaultValue;
166 }
167 }
168
169 return $value;
170 }
171
172 /**
173 * Recursively translate values.
174 *
175 * @param array $array
176 * @param string $translationFile
177 * @return array the modified array
178 * @internal
179 */
180 public function translateValuesRecursive(array $array, string $translationFile = null): array
181 {
182 $result = $array;
183 foreach ($result as $key => $value) {
184 if (is_array($value)) {
185 $result[$key] = $this->translateValuesRecursive($value, $translationFile);
186 } else {
187 $result[$key] = $this->translate($value, null, $translationFile, null, $value);
188 }
189 }
190 return $result;
191 }
192
193 /**
194 * @param FormRuntime $formRuntime
195 * @param string $finisherIdentifier
196 * @param string $optionKey
197 * @param string $optionValue
198 * @param array $renderingOptions
199 * @return string
200 * @throws \InvalidArgumentException
201 * @api
202 */
203 public function translateFinisherOption(
204 FormRuntime $formRuntime,
205 string $finisherIdentifier,
206 string $optionKey,
207 string $optionValue,
208 array $renderingOptions = []
209 ): string {
210 if (empty($finisherIdentifier)) {
211 throw new \InvalidArgumentException('The argument "finisherIdentifier" is empty', 1476216059);
212 }
213 if (empty($optionKey)) {
214 throw new \InvalidArgumentException('The argument "optionKey" is empty', 1476216060);
215 }
216
217 $finisherIdentifier = preg_replace('/Finisher$/', '', $finisherIdentifier);
218 $translationFile = $renderingOptions['translationFile'];
219 if (isset($renderingOptions['translatePropertyValueIfEmpty'])) {
220 $translatePropertyValueIfEmpty = (bool)$renderingOptions['translatePropertyValueIfEmpty'];
221 } else {
222 $translatePropertyValueIfEmpty = true;
223 }
224
225 if (empty($optionValue) && !$translatePropertyValueIfEmpty) {
226 return $optionValue;
227 }
228
229 $language = null;
230 if (isset($renderingOptions['language'])) {
231 $language = $renderingOptions['language'];
232 }
233
234 $translationKeyChain = [
235 sprintf('%s:%s.finisher.%s.%s', $translationFile, $formRuntime->getIdentifier(), $finisherIdentifier, $optionKey),
236 sprintf('%s:finisher.%s.%s', $translationFile, $finisherIdentifier, $optionKey)
237 ];
238 $translatedValue = $this->processTranslationChain($translationKeyChain, $language);
239 $translatedValue = (empty($translatedValue)) ? $optionValue : $translatedValue;
240
241 return $translatedValue;
242 }
243
244 /**
245 * @param RootRenderableInterface $element
246 * @param string $property
247 * @param FormRuntime $formRuntime
248 * @return string|array
249 * @throws \InvalidArgumentException
250 * @internal
251 */
252 public function translateFormElementValue(
253 RootRenderableInterface $element,
254 string $property,
255 FormRuntime $formRuntime
256 ) {
257 if (empty($property)) {
258 throw new \InvalidArgumentException('The argument "property" is empty', 1476216007);
259 }
260
261 $propertyType = 'properties';
262 $renderingOptions = $element->getRenderingOptions();
263
264 if ($property === 'label') {
265 $defaultValue = $element->getLabel();
266 } else {
267 if ($element instanceof FormElementInterface) {
268 $defaultValue = ArrayUtility::getValueByPath($element->getProperties(), $property);
269 } else {
270 $propertyType = 'renderingOptions';
271 $defaultValue = ArrayUtility::getValueByPath($renderingOptions, $property);
272 }
273 }
274
275 if (isset($renderingOptions['translation']['translatePropertyValueIfEmpty'])) {
276 $translatePropertyValueIfEmpty = $renderingOptions['translation']['translatePropertyValueIfEmpty'];
277 } else {
278 $translatePropertyValueIfEmpty = true;
279 }
280
281 if (empty($defaultValue) && !$translatePropertyValueIfEmpty) {
282 return $defaultValue;
283 }
284
285 $defaultValue = empty($defaultValue) ? '' : $defaultValue;
286 $translationFile = $renderingOptions['translation']['translationFile'];
287
288 $language = null;
289 if (isset($renderingOptions['translation']['language'])) {
290 $language = $renderingOptions['translation']['language'];
291 }
292 $translationKeyChain = [];
293 if ($property === 'options' && is_array($defaultValue)) {
294 foreach ($defaultValue as $optionValue => &$optionLabel) {
295 $translationKeyChain = [
296 sprintf('%s:%s.element.%s.%s.%s.%s', $translationFile, $formRuntime->getIdentifier(), $element->getIdentifier(), $propertyType, $property, $optionValue),
297 sprintf('%s:element.%s.%s.%s.%s', $translationFile, $element->getIdentifier(), $propertyType, $property, $optionValue)
298 ];
299 $translatedValue = $this->processTranslationChain($translationKeyChain, $language);
300 $optionLabel = (empty($translatedValue)) ? $optionLabel : $translatedValue;
301 }
302 $translatedValue = $defaultValue;
303 } else {
304 $translationKeyChain = [
305 sprintf('%s:%s.element.%s.%s.%s', $translationFile, $formRuntime->getIdentifier(), $element->getIdentifier(), $propertyType, $property),
306 sprintf('%s:element.%s.%s.%s', $translationFile, $element->getIdentifier(), $propertyType, $property),
307 sprintf('%s:element.%s.%s.%s', $translationFile, $element->getType(), $propertyType, $property),
308 ];
309 $translatedValue = $this->processTranslationChain($translationKeyChain, $language);
310 $translatedValue = (empty($translatedValue)) ? $defaultValue : $translatedValue;
311 }
312
313 return $translatedValue;
314 }
315
316 /**
317 * @param string $languageKey
318 * @internal
319 */
320 public function setLanguage(string $languageKey)
321 {
322 $this->languageKey = $languageKey;
323 }
324
325 /**
326 * @return string
327 * @internal
328 */
329 public function getLanguage(): string
330 {
331 return $this->languageKey;
332 }
333
334 /**
335 * @param array $translationKeyChain
336 * @param string $language
337 * @return string|null
338 */
339 protected function processTranslationChain(array $translationKeyChain, string $language = null)
340 {
341 $translatedValue = null;
342 foreach ($translationKeyChain as $translationKey) {
343 $translatedValue = $this->translate($translationKey, null, null, $language);
344 if (!empty($translatedValue)) {
345 break;
346 }
347 }
348 return $translatedValue;
349 }
350
351 /**
352 * @param string $locallangPathAndFilename
353 * @return void
354 */
355 protected function initializeLocalization(string $locallangPathAndFilename)
356 {
357 if (empty($this->languageKey)) {
358 $this->setLanguageKeys();
359 }
360
361 if (!empty($locallangPathAndFilename)) {
362 /** @var $languageFactory LocalizationFactory */
363 $languageFactory = GeneralUtility::makeInstance(LocalizationFactory::class);
364
365 $this->LOCAL_LANG = $languageFactory->getParsedData($locallangPathAndFilename, $this->languageKey, 'utf-8');
366
367 foreach ($this->alternativeLanguageKeys as $language) {
368 $tempLL = $languageFactory->getParsedData($locallangPathAndFilename, $language, 'utf-8');
369 if ($this->languageKey !== 'default' && isset($tempLL[$language])) {
370 $this->LOCAL_LANG[$language] = $tempLL[$language];
371 }
372 }
373 }
374 $this->loadTypoScriptLabels();
375 }
376
377 /**
378 * Sets the currently active language/language_alt keys.
379 * Default values are "default" for language key and "" for language_alt key.
380 *
381 * @return void
382 */
383 protected function setLanguageKeys()
384 {
385 $this->languageKey = 'default';
386
387 $this->alternativeLanguageKeys = [];
388 if (TYPO3_MODE === 'FE') {
389 if (isset($this->getTypoScriptFrontendController()->config['config']['language'])) {
390 $this->languageKey = $this->getTypoScriptFrontendController()->config['config']['language'];
391 if (isset($this->getTypoScriptFrontendController()->config['config']['language_alt'])) {
392 $this->alternativeLanguageKeys[] = $this->getTypoScriptFrontendController()->config['config']['language_alt'];
393 } else {
394 /** @var $locales \TYPO3\CMS\Core\Localization\Locales */
395 $locales = GeneralUtility::makeInstance(Locales::class);
396 if (in_array($this->languageKey, $locales->getLocales(), true)) {
397 foreach ($locales->getLocaleDependencies($this->languageKey) as $language) {
398 $this->alternativeLanguageKeys[] = $language;
399 }
400 }
401 }
402 }
403 } elseif (!empty($GLOBALS['BE_USER']->uc['lang'])) {
404 $this->languageKey = $GLOBALS['BE_USER']->uc['lang'];
405 } elseif (!empty($this->getLanguageService()->lang)) {
406 $this->languageKey = $this->getLanguageService()->lang;
407 }
408 }
409
410 /**
411 * Overwrites labels that are set via TypoScript.
412 * TS locallang labels have to be configured like:
413 * plugin.tx_form._LOCAL_LANG.languageKey.key = value
414 *
415 * @return void
416 */
417 protected function loadTypoScriptLabels()
418 {
419 $frameworkConfiguration = $this->getConfigurationManager()
420 ->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK, 'form');
421
422 if (!is_array($frameworkConfiguration['_LOCAL_LANG'])) {
423 return;
424 }
425 $this->LOCAL_LANG_UNSET = [];
426 foreach ($frameworkConfiguration['_LOCAL_LANG'] as $languageKey => $labels) {
427 if (!(is_array($labels) && isset($this->LOCAL_LANG[$languageKey]))) {
428 continue;
429 }
430 foreach ($labels as $labelKey => $labelValue) {
431 if (is_string($labelValue)) {
432 $this->LOCAL_LANG[$languageKey][$labelKey][0]['target'] = $labelValue;
433 if ($labelValue === '') {
434 $this->LOCAL_LANG_UNSET[$languageKey][$labelKey] = '';
435 }
436 } elseif (is_array($labelValue)) {
437 $labelValue = $this->flattenTypoScriptLabelArray($labelValue, $labelKey);
438 foreach ($labelValue as $key => $value) {
439 $this->LOCAL_LANG[$languageKey][$key][0]['target'] = $value;
440 if ($value === '') {
441 $this->LOCAL_LANG_UNSET[$languageKey][$key] = '';
442 }
443 }
444 }
445 }
446 }
447 }
448
449 /**
450 * Flatten TypoScript label array; converting a hierarchical array into a flat
451 * array with the keys separated by dots.
452 *
453 * Example Input: array('k1' => array('subkey1' => 'val1'))
454 * Example Output: array('k1.subkey1' => 'val1')
455 *
456 * @param array $labelValues Hierarchical array of labels
457 * @param string $parentKey the name of the parent key in the recursion; is only needed for recursion.
458 * @return array flattened array of labels.
459 */
460 protected function flattenTypoScriptLabelArray(array $labelValues, string $parentKey = ''): array
461 {
462 $result = [];
463 foreach ($labelValues as $key => $labelValue) {
464 if (!empty($parentKey)) {
465 $key = $parentKey . '.' . $key;
466 }
467 if (is_array($labelValue)) {
468 $labelValue = $this->flattenTypoScriptLabelArray($labelValue, $key);
469 $result = array_merge($result, $labelValue);
470 } else {
471 $result[$key] = $labelValue;
472 }
473 }
474 return $result;
475 }
476
477 /**
478 * Returns instance of the configuration manager
479 *
480 * @return ConfigurationManagerInterface
481 */
482 protected function getConfigurationManager(): ConfigurationManagerInterface
483 {
484 if ($this->configurationManager !== null) {
485 return $this->configurationManager;
486 }
487
488 $this->configurationManager = GeneralUtility::makeInstance(ObjectManager::class)
489 ->get(ConfigurationManagerInterface::class);
490 return $this->configurationManager;
491 }
492
493 /**
494 * @return LanguageService
495 */
496 protected function getLanguageService(): LanguageService
497 {
498 return $GLOBALS['LANG'];
499 }
500
501 /**
502 * @return TypoScriptFrontendController
503 */
504 protected function getTypoScriptFrontendController(): TypoScriptFrontendController
505 {
506 return $GLOBALS['TSFE'];
507 }
508 }