Completed
Push — master ( 2f2cff...7971bc )
by
unknown
26:04
created

LocalizationUtility   C

Complexity

Total Complexity 55

Size/Duplication

Total Lines 292
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 114
c 1
b 0
f 0
dl 0
loc 292
rs 6
wmc 55

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getLanguageFilePath() 0 3 1
B initializeLocalization() 0 24 9
D translate() 0 64 18
A flattenTypoScriptLabelArray() 0 19 5
A getLanguageService() 0 3 1
B getLanguageKeys() 0 26 7
B loadTypoScriptLabels() 0 24 10
A getConfigurationManager() 0 9 2
A getCurrentSiteLanguage() 0 6 2

How to fix   Complexity   

Complex Class

Complex classes like LocalizationUtility often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use LocalizationUtility, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
declare(strict_types=1);
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
namespace TYPO3\CMS\Extbase\Utility;
19
20
use Psr\Http\Message\ServerRequestInterface;
21
use TYPO3\CMS\Core\Localization\LanguageService;
22
use TYPO3\CMS\Core\Localization\Locales;
23
use TYPO3\CMS\Core\Localization\LocalizationFactory;
24
use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
25
use TYPO3\CMS\Core\Utility\GeneralUtility;
26
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
27
use TYPO3\CMS\Extbase\Object\ObjectManager;
28
29
/**
30
 * Localization helper which should be used to fetch localized labels.
31
 */
32
class LocalizationUtility
33
{
34
    /**
35
     * @var string
36
     */
37
    protected static $locallangPath = 'Resources/Private/Language/';
38
39
    /**
40
     * Local Language content
41
     *
42
     * @var array
43
     */
44
    protected static $LOCAL_LANG = [];
45
46
    /**
47
     * Contains those LL keys, which have been set to (empty) in TypoScript.
48
     * This is necessary, as we cannot distinguish between a nonexisting
49
     * translation and a label that has been cleared by TS.
50
     * In both cases ['key'][0]['target'] is "".
51
     *
52
     * @var array
53
     */
54
    protected static $LOCAL_LANG_UNSET = [];
55
56
    /**
57
     * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
58
     */
59
    protected static $configurationManager;
60
61
    /**
62
     * Returns the localized label of the LOCAL_LANG key, $key.
63
     *
64
     * @param string $key The key from the LOCAL_LANG array for which to return the value.
65
     * @param string|null $extensionName The name of the extension
66
     * @param array $arguments The arguments of the extension, being passed over to vsprintf
67
     * @param string $languageKey The language key or null for using the current language from the system
68
     * @param string[] $alternativeLanguageKeys The alternative language keys if no translation was found.
69
     * @return string|null The value from LOCAL_LANG or null if no translation was found.
70
     */
71
    public static function translate(string $key, ?string $extensionName = null, array $arguments = null, string $languageKey = null, array $alternativeLanguageKeys = null): ?string
72
    {
73
        if ($key === '') {
74
            // Early return guard: returns null if the key was empty, because the key may be a dynamic value
75
            // (from for example Fluid). Returning null allows null coalescing to a default value when that happens.
76
            return null;
77
        }
78
        $value = null;
79
        if (GeneralUtility::isFirstPartOfStr($key, 'LLL:')) {
80
            $keyParts = explode(':', $key);
81
            unset($keyParts[0]);
82
            $key = array_pop($keyParts);
83
            $languageFilePath = implode(':', $keyParts);
84
        } else {
85
            if (empty($extensionName)) {
86
                throw new \InvalidArgumentException(
87
                    'Parameter $extensionName cannot be empty if a fully-qualified key is not specified.',
88
                    1498144052
89
                );
90
            }
91
            $languageFilePath = static::getLanguageFilePath($extensionName);
92
        }
93
        $languageKeys = static::getLanguageKeys();
94
        if ($languageKey === null) {
95
            $languageKey = $languageKeys['languageKey'];
96
        }
97
        if (empty($alternativeLanguageKeys)) {
98
            $alternativeLanguageKeys = $languageKeys['alternativeLanguageKeys'];
99
        }
100
        static::initializeLocalization($languageFilePath, $languageKey, $alternativeLanguageKeys, $extensionName);
101
102
        // The "from" charset of csConv() is only set for strings from TypoScript via _LOCAL_LANG
103
        if (!empty(self::$LOCAL_LANG[$languageFilePath][$languageKey][$key][0]['target'])
104
            || isset(self::$LOCAL_LANG_UNSET[$languageFilePath][$languageKey][$key])
105
        ) {
106
            // Local language translation for key exists
107
            $value = self::$LOCAL_LANG[$languageFilePath][$languageKey][$key][0]['target'];
108
        } elseif (!empty($alternativeLanguageKeys)) {
109
            $languages = array_reverse($alternativeLanguageKeys);
110
            foreach ($languages as $language) {
111
                if (!empty(self::$LOCAL_LANG[$languageFilePath][$language][$key][0]['target'])
112
                    || isset(self::$LOCAL_LANG_UNSET[$languageFilePath][$language][$key])
113
                ) {
114
                    // Alternative language translation for key exists
115
                    $value = self::$LOCAL_LANG[$languageFilePath][$language][$key][0]['target'];
116
                    break;
117
                }
118
            }
119
        }
120
        if ($value === null && (!empty(self::$LOCAL_LANG[$languageFilePath]['default'][$key][0]['target'])
121
            || isset(self::$LOCAL_LANG_UNSET[$languageFilePath]['default'][$key]))
122
        ) {
123
            // Default language translation for key exists
124
            // No charset conversion because default is English and thereby ASCII
125
            $value = self::$LOCAL_LANG[$languageFilePath]['default'][$key][0]['target'];
126
        }
127
128
        if (is_array($arguments) && $value !== null) {
129
            // This unrolls arguments from $arguments - instead of calling vsprintf which receives arguments as an array.
130
            // The reason is that only sprintf() will return an error message if the number of arguments does not match
131
            // the number of placeholders in the format string. Whereas, vsprintf would silently return nothing.
132
            return sprintf($value, ...array_values($arguments)) ?: sprintf('Error: could not translate key "%s" with value "%s" and %d argument(s)!', $key, $value, count($arguments));
133
        }
134
        return $value;
135
    }
136
137
    /**
138
     * Loads local-language values by looking for a "locallang.xlf" (or "locallang.xml") file in the plugin resources directory and if found includes it.
139
     * Also locallang values set in the TypoScript property "_LOCAL_LANG" are merged onto the values found in the "locallang.xlf" file.
140
     *
141
     * @param string $languageFilePath
142
     * @param string $languageKey
143
     * @param string[] $alternativeLanguageKeys
144
     * @param string $extensionName
145
     */
146
    protected static function initializeLocalization(string $languageFilePath, string $languageKey, array $alternativeLanguageKeys, string $extensionName = null): void
147
    {
148
        $languageFactory = GeneralUtility::makeInstance(LocalizationFactory::class);
149
150
        if (empty(self::$LOCAL_LANG[$languageFilePath][$languageKey])) {
151
            $parsedData = $languageFactory->getParsedData($languageFilePath, $languageKey);
152
            foreach ($parsedData as $tempLanguageKey => $data) {
153
                if (!empty($data)) {
154
                    self::$LOCAL_LANG[$languageFilePath][$tempLanguageKey] = $data;
155
                }
156
            }
157
        }
158
        if ($languageKey !== 'default') {
159
            foreach ($alternativeLanguageKeys as $alternativeLanguageKey) {
160
                if (empty(self::$LOCAL_LANG[$languageFilePath][$alternativeLanguageKey])) {
161
                    $tempLL = $languageFactory->getParsedData($languageFilePath, $alternativeLanguageKey);
162
                    if (isset($tempLL[$alternativeLanguageKey])) {
163
                        self::$LOCAL_LANG[$languageFilePath][$alternativeLanguageKey] = $tempLL[$alternativeLanguageKey];
164
                    }
165
                }
166
            }
167
        }
168
        if (!empty($extensionName)) {
169
            static::loadTypoScriptLabels($extensionName, $languageFilePath);
170
        }
171
    }
172
173
    /**
174
     * Returns the default path and filename for an extension
175
     *
176
     * @param string $extensionName
177
     * @return string
178
     */
179
    protected static function getLanguageFilePath(string $extensionName): string
180
    {
181
        return 'EXT:' . GeneralUtility::camelCaseToLowerCaseUnderscored($extensionName) . '/' . self::$locallangPath . 'locallang.xlf';
182
    }
183
184
    /**
185
     * Sets the currently active language keys.
186
     *
187
     * @return array
188
     */
189
    protected static function getLanguageKeys(): array
190
    {
191
        $languageKeys = [
192
            'languageKey' => 'default',
193
            'alternativeLanguageKeys' => [],
194
        ];
195
        if (TYPO3_MODE === 'FE') {
0 ignored issues
show
introduced by
The condition TYPO3\CMS\Extbase\Utility\TYPO3_MODE === 'FE' is always false.
Loading history...
196
            $siteLanguage = self::getCurrentSiteLanguage();
197
198
            // Get values from site language
199
            if ($siteLanguage !== null) {
200
                $languageKeys['languageKey'] = $siteLanguage->getTypo3Language();
201
            }
202
203
            $locales = GeneralUtility::makeInstance(Locales::class);
204
            if (in_array($languageKeys['languageKey'], $locales->getLocales())) {
205
                foreach ($locales->getLocaleDependencies($languageKeys['languageKey']) as $language) {
206
                    $languageKeys['alternativeLanguageKeys'][] = $language;
207
                }
208
            }
209
        } elseif (!empty($GLOBALS['BE_USER']->uc['lang'])) {
210
            $languageKeys['languageKey'] = $GLOBALS['BE_USER']->uc['lang'];
211
        } elseif (!empty(static::getLanguageService()->lang)) {
212
            $languageKeys['languageKey'] = static::getLanguageService()->lang;
213
        }
214
        return $languageKeys;
215
    }
216
217
    /**
218
     * Overwrites labels that are set via TypoScript.
219
     * TS locallang labels have to be configured like:
220
     * plugin.tx_myextension._LOCAL_LANG.languageKey.key = value
221
     *
222
     * @param string $extensionName
223
     * @param string $languageFilePath
224
     */
225
    protected static function loadTypoScriptLabels(string $extensionName, string $languageFilePath): void
226
    {
227
        $configurationManager = static::getConfigurationManager();
228
        $frameworkConfiguration = $configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK, $extensionName);
229
        if (!is_array($frameworkConfiguration['_LOCAL_LANG'] ?? false)) {
230
            return;
231
        }
232
        self::$LOCAL_LANG_UNSET[$languageFilePath] = [];
233
        foreach ($frameworkConfiguration['_LOCAL_LANG'] as $languageKey => $labels) {
234
            if (!is_array($labels)) {
235
                continue;
236
            }
237
            foreach ($labels as $labelKey => $labelValue) {
238
                if (is_string($labelValue)) {
239
                    self::$LOCAL_LANG[$languageFilePath][$languageKey][$labelKey][0]['target'] = $labelValue;
240
                    if ($labelValue === '') {
241
                        self::$LOCAL_LANG_UNSET[$languageFilePath][$languageKey][$labelKey] = '';
242
                    }
243
                } elseif (is_array($labelValue)) {
244
                    $labelValue = self::flattenTypoScriptLabelArray($labelValue, $labelKey);
245
                    foreach ($labelValue as $key => $value) {
246
                        self::$LOCAL_LANG[$languageFilePath][$languageKey][$key][0]['target'] = $value;
247
                        if ($value === '') {
248
                            self::$LOCAL_LANG_UNSET[$languageFilePath][$languageKey][$key] = '';
249
                        }
250
                    }
251
                }
252
            }
253
        }
254
    }
255
256
    /**
257
     * Flatten TypoScript label array; converting a hierarchical array into a flat
258
     * array with the keys separated by dots.
259
     *
260
     * Example Input:  array('k1' => array('subkey1' => 'val1'))
261
     * Example Output: array('k1.subkey1' => 'val1')
262
     *
263
     * @param array $labelValues Hierarchical array of labels
264
     * @param string $parentKey the name of the parent key in the recursion; is only needed for recursion.
265
     * @return array flattened array of labels.
266
     */
267
    protected static function flattenTypoScriptLabelArray(array $labelValues, string $parentKey = ''): array
268
    {
269
        $result = [];
270
        foreach ($labelValues as $key => $labelValue) {
271
            if (!empty($parentKey)) {
272
                if ($key === '_typoScriptNodeValue') {
273
                    $key = $parentKey;
274
                } else {
275
                    $key = $parentKey . '.' . $key;
276
                }
277
            }
278
            if (is_array($labelValue)) {
279
                $labelValue = self::flattenTypoScriptLabelArray($labelValue, $key);
280
                $result = array_merge($result, $labelValue);
281
            } else {
282
                $result[$key] = $labelValue;
283
            }
284
        }
285
        return $result;
286
    }
287
288
    /**
289
     * Returns instance of the configuration manager
290
     *
291
     * @return \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
292
     */
293
    protected static function getConfigurationManager(): ConfigurationManagerInterface
294
    {
295
        if (static::$configurationManager !== null) {
296
            return static::$configurationManager;
297
        }
298
        $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
299
        $configurationManager = $objectManager->get(ConfigurationManagerInterface::class);
300
        static::$configurationManager = $configurationManager;
301
        return $configurationManager;
302
    }
303
304
    /**
305
     * Returns the currently configured "site language" if a site is configured (= resolved)
306
     * in the current request.
307
     *
308
     * @return SiteLanguage|null
309
     */
310
    protected static function getCurrentSiteLanguage(): ?SiteLanguage
311
    {
312
        if ($GLOBALS['TYPO3_REQUEST'] instanceof ServerRequestInterface) {
313
            return $GLOBALS['TYPO3_REQUEST']->getAttribute('language', null);
314
        }
315
        return null;
316
    }
317
318
    /**
319
     * @return \TYPO3\CMS\Core\Localization\LanguageService
320
     */
321
    protected static function getLanguageService(): LanguageService
322
    {
323
        return $GLOBALS['LANG'];
324
    }
325
}
326