getParsedData($languageFilePath, $languageKey); foreach ($parsedData as $tempLanguageKey => $data) { if (!empty($data)) { self::$LOCAL_LANG[$languageFilePath][$tempLanguageKey] = $data; } } } if ($languageKey !== 'default') { foreach ($alternativeLanguageKeys as $alternativeLanguageKey) { if (empty(self::$LOCAL_LANG[$languageFilePath][$alternativeLanguageKey])) { $tempLL = $languageFactory->getParsedData($languageFilePath, $alternativeLanguageKey); if (isset($tempLL[$alternativeLanguageKey])) { self::$LOCAL_LANG[$languageFilePath][$alternativeLanguageKey] = $tempLL[$alternativeLanguageKey]; } } } } if (!empty($extensionName)) { static::loadTypoScriptLabels($extensionName, $languageFilePath); } } /** * Returns the default path and filename for an extension * * @param string $extensionName * @return string */ protected static function getLanguageFilePath(string $extensionName): string { return 'EXT:' . GeneralUtility::camelCaseToLowerCaseUnderscored($extensionName) . '/' . self::$locallangPath . 'locallang.xlf'; } /** * Sets the currently active language/language_alt keys. * Default values are "default" for language key and an empty array for language_alt key. * * @return array */ protected static function getLanguageKeys(): array { $languageKeys = [ 'languageKey' => 'default', 'alternativeLanguageKeys' => [], ]; if (TYPO3_MODE === 'FE') { $tsfe = static::getTypoScriptFrontendController(); $siteLanguage = self::getCurrentSiteLanguage(); // Get values from site language, which takes precedence over TypoScript settings if ($siteLanguage instanceof SiteLanguage) { $languageKeys['languageKey'] = $siteLanguage->getTypo3Language(); } elseif (isset($tsfe->config['config']['language'])) { $languageKeys['languageKey'] = $tsfe->config['config']['language']; if (isset($tsfe->config['config']['language_alt'])) { $languageKeys['alternativeLanguageKeys'] = $tsfe->config['config']['language_alt']; } } if (empty($languageKeys['alternativeLanguageKeys'])) { $locales = GeneralUtility::makeInstance(Locales::class); if (in_array($languageKeys['languageKey'], $locales->getLocales())) { foreach ($locales->getLocaleDependencies($languageKeys['languageKey']) as $language) { $languageKeys['alternativeLanguageKeys'] = $language; } } } } elseif (!empty($GLOBALS['BE_USER']->uc['lang'])) { $languageKeys['languageKey'] = $GLOBALS['BE_USER']->uc['lang']; } elseif (!empty(static::getLanguageService()->lang)) { $languageKeys['languageKey'] = static::getLanguageService()->lang; } return $languageKeys; } /** * Overwrites labels that are set via TypoScript. * TS locallang labels have to be configured like: * plugin.tx_myextension._LOCAL_LANG.languageKey.key = value * * @param string $extensionName * @param string $languageFilePath */ protected static function loadTypoScriptLabels($extensionName, $languageFilePath) { $configurationManager = static::getConfigurationManager(); $frameworkConfiguration = $configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK, $extensionName); if (!is_array($frameworkConfiguration['_LOCAL_LANG'] ?? false)) { return; } self::$LOCAL_LANG_UNSET[$languageFilePath] = []; foreach ($frameworkConfiguration['_LOCAL_LANG'] as $languageKey => $labels) { if (!is_array($labels)) { continue; } foreach ($labels as $labelKey => $labelValue) { if (is_string($labelValue)) { self::$LOCAL_LANG[$languageFilePath][$languageKey][$labelKey][0]['target'] = $labelValue; if ($labelValue === '') { self::$LOCAL_LANG_UNSET[$languageFilePath][$languageKey][$labelKey] = ''; } } elseif (is_array($labelValue)) { $labelValue = self::flattenTypoScriptLabelArray($labelValue, $labelKey); foreach ($labelValue as $key => $value) { self::$LOCAL_LANG[$languageFilePath][$languageKey][$key][0]['target'] = $value; if ($value === '') { self::$LOCAL_LANG_UNSET[$languageFilePath][$languageKey][$key] = ''; } } } } } } /** * Flatten TypoScript label array; converting a hierarchical array into a flat * array with the keys separated by dots. * * Example Input: array('k1' => array('subkey1' => 'val1')) * Example Output: array('k1.subkey1' => 'val1') * * @param array $labelValues Hierarchical array of labels * @param string $parentKey the name of the parent key in the recursion; is only needed for recursion. * @return array flattened array of labels. */ protected static function flattenTypoScriptLabelArray(array $labelValues, $parentKey = '') { $result = []; foreach ($labelValues as $key => $labelValue) { if (!empty($parentKey)) { if ($key === '_typoScriptNodeValue') { $key = $parentKey; } else { $key = $parentKey . '.' . $key; } } if (is_array($labelValue)) { $labelValue = self::flattenTypoScriptLabelArray($labelValue, $key); $result = array_merge($result, $labelValue); } else { $result[$key] = $labelValue; } } return $result; } /** * Returns instance of the configuration manager * * @return \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface */ protected static function getConfigurationManager() { if (static::$configurationManager !== null) { return static::$configurationManager; } $objectManager = GeneralUtility::makeInstance(ObjectManager::class); $configurationManager = $objectManager->get(ConfigurationManagerInterface::class); static::$configurationManager = $configurationManager; return $configurationManager; } /** * Returns the currently configured "site language" if a site is configured (= resolved) * in the current request. * * @return SiteLanguage|null */ protected static function getCurrentSiteLanguage(): ?SiteLanguage { if ($GLOBALS['TYPO3_REQUEST'] instanceof ServerRequestInterface) { return $GLOBALS['TYPO3_REQUEST']->getAttribute('language', null); } return null; } /** * @return \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController */ protected static function getTypoScriptFrontendController() { return $GLOBALS['TSFE']; } /** * @return \TYPO3\CMS\Core\Localization\LanguageService */ protected static function getLanguageService() { return $GLOBALS['LANG']; } }