LocalizationUtility::translate()   D
last analyzed

Complexity

Conditions 18
Paths 162

Size

Total Lines 64
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 18
eloc 36
nc 162
nop 5
dl 0
loc 64
rs 4.35
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Http\ApplicationType;
22
use TYPO3\CMS\Core\Localization\LanguageService;
23
use TYPO3\CMS\Core\Localization\Locales;
24
use TYPO3\CMS\Core\Localization\LocalizationFactory;
25
use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
26
use TYPO3\CMS\Core\Utility\GeneralUtility;
27
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
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" 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 (($GLOBALS['TYPO3_REQUEST'] ?? null) instanceof ServerRequestInterface
196
            && ApplicationType::fromRequest($GLOBALS['TYPO3_REQUEST'])->isFrontend()
197
        ) {
198
            // Frontend application
199
            $siteLanguage = self::getCurrentSiteLanguage();
200
201
            // Get values from site language
202
            if ($siteLanguage !== null) {
203
                $languageKeys['languageKey'] = $siteLanguage->getTypo3Language();
204
            }
205
206
            $locales = GeneralUtility::makeInstance(Locales::class);
207
            if (in_array($languageKeys['languageKey'], $locales->getLocales())) {
208
                foreach ($locales->getLocaleDependencies($languageKeys['languageKey']) as $language) {
209
                    $languageKeys['alternativeLanguageKeys'][] = $language;
210
                }
211
            }
212
        } elseif (!empty($GLOBALS['BE_USER']->user['lang'])) {
213
            $languageKeys['languageKey'] = $GLOBALS['BE_USER']->user['lang'];
214
        } elseif (!empty(static::getLanguageService()->lang)) {
215
            $languageKeys['languageKey'] = static::getLanguageService()->lang;
216
        }
217
        return $languageKeys;
218
    }
219
220
    /**
221
     * Overwrites labels that are set via TypoScript.
222
     * TS locallang labels have to be configured like:
223
     * plugin.tx_myextension._LOCAL_LANG.languageKey.key = value
224
     *
225
     * @param string $extensionName
226
     * @param string $languageFilePath
227
     */
228
    protected static function loadTypoScriptLabels(string $extensionName, string $languageFilePath): void
229
    {
230
        $configurationManager = static::getConfigurationManager();
231
        $frameworkConfiguration = $configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK, $extensionName);
232
        if (!is_array($frameworkConfiguration['_LOCAL_LANG'] ?? false)) {
233
            return;
234
        }
235
        self::$LOCAL_LANG_UNSET[$languageFilePath] = [];
236
        foreach ($frameworkConfiguration['_LOCAL_LANG'] as $languageKey => $labels) {
237
            if (!is_array($labels)) {
238
                continue;
239
            }
240
            foreach ($labels as $labelKey => $labelValue) {
241
                if (is_string($labelValue)) {
242
                    self::$LOCAL_LANG[$languageFilePath][$languageKey][$labelKey][0]['target'] = $labelValue;
243
                    if ($labelValue === '') {
244
                        self::$LOCAL_LANG_UNSET[$languageFilePath][$languageKey][$labelKey] = '';
245
                    }
246
                } elseif (is_array($labelValue)) {
247
                    $labelValue = self::flattenTypoScriptLabelArray($labelValue, $labelKey);
248
                    foreach ($labelValue as $key => $value) {
249
                        self::$LOCAL_LANG[$languageFilePath][$languageKey][$key][0]['target'] = $value;
250
                        if ($value === '') {
251
                            self::$LOCAL_LANG_UNSET[$languageFilePath][$languageKey][$key] = '';
252
                        }
253
                    }
254
                }
255
            }
256
        }
257
    }
258
259
    /**
260
     * Flatten TypoScript label array; converting a hierarchical array into a flat
261
     * array with the keys separated by dots.
262
     *
263
     * Example Input:  array('k1' => array('subkey1' => 'val1'))
264
     * Example Output: array('k1.subkey1' => 'val1')
265
     *
266
     * @param array $labelValues Hierarchical array of labels
267
     * @param string $parentKey the name of the parent key in the recursion; is only needed for recursion.
268
     * @return array flattened array of labels.
269
     */
270
    protected static function flattenTypoScriptLabelArray(array $labelValues, string $parentKey = ''): array
271
    {
272
        $result = [];
273
        foreach ($labelValues as $key => $labelValue) {
274
            if (!empty($parentKey)) {
275
                if ($key === '_typoScriptNodeValue') {
276
                    $key = $parentKey;
277
                } else {
278
                    $key = $parentKey . '.' . $key;
279
                }
280
            }
281
            if (is_array($labelValue)) {
282
                $labelValue = self::flattenTypoScriptLabelArray($labelValue, $key);
283
                $result = array_merge($result, $labelValue);
284
            } else {
285
                $result[$key] = $labelValue;
286
            }
287
        }
288
        return $result;
289
    }
290
291
    /**
292
     * Returns instance of the configuration manager
293
     *
294
     * @return \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
295
     */
296
    protected static function getConfigurationManager(): ConfigurationManagerInterface
297
    {
298
        if (static::$configurationManager !== null) {
299
            return static::$configurationManager;
300
        }
301
        static::$configurationManager = GeneralUtility::makeInstance(ConfigurationManagerInterface::class);
302
        return static::$configurationManager;
303
    }
304
305
    /**
306
     * Returns the currently configured "site language" if a site is configured (= resolved)
307
     * in the current request.
308
     *
309
     * @return SiteLanguage|null
310
     */
311
    protected static function getCurrentSiteLanguage(): ?SiteLanguage
312
    {
313
        if ($GLOBALS['TYPO3_REQUEST'] instanceof ServerRequestInterface) {
314
            return $GLOBALS['TYPO3_REQUEST']->getAttribute('language', null);
315
        }
316
        return null;
317
    }
318
319
    /**
320
     * @return \TYPO3\CMS\Core\Localization\LanguageService
321
     */
322
    protected static function getLanguageService(): LanguageService
323
    {
324
        return $GLOBALS['LANG'];
325
    }
326
}
327