Total Complexity | 56 |
Total Lines | 293 |
Duplicated Lines | 0 % |
Changes | 0 |
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 |
||
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 |
||
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 |
||
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 |
||
325 | } |
||
326 | } |
||
327 |