Created
February 4, 2018 18:48
-
-
Save derhansen/1eee357e472be595692bf655a28a1557 to your computer and use it in GitHub Desktop.
Extended LocalizationUtility for TYPO3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Vendor\Namespace\Utility; | |
use TYPO3\CMS\Extbase\Utility\LocalizationUtility as LocalizationUtilityExtbase; | |
/** | |
* Class LocalizationUtility | |
*/ | |
class LocalizationUtility extends LocalizationUtilityExtbase | |
{ | |
/** | |
* Returns the localized label of the LOCAL_LANG key, | |
* but prefill extensionName | |
* and adds the possibility to set temporarily a particular language key | |
* | |
* @param string $key The key from the LOCAL_LANG array for which to return the value. | |
* @param string $extensionName The name of the extension | |
* @param array $arguments the arguments of the extension, being passed over to vsprintf | |
* @param string $languageKey | |
* @return string|null | |
*/ | |
public static function translate($key, $extensionName = 'extensionkey', $arguments = null, $languageKey = '') | |
{ | |
// Change the language key temporarily | |
if ($languageKey != '') { | |
$currentLanguageKey = self::$languageKey; | |
self::$languageKey = $languageKey; | |
} | |
$translatedString = parent::translate($key, $extensionName, $arguments); | |
// set the language key from initializeLocalization | |
if ($languageKey != '') { | |
self::$languageKey = $currentLanguageKey; | |
} | |
return $translatedString; | |
} | |
/** | |
* Easy method to change the initialized language | |
* | |
* @param string $languageKey | |
* @param string $extensionName | |
*/ | |
public static function setLanguageKey($languageKey = '', $extensionName = 'example') | |
{ | |
/* | |
* important to initialize self::$LOCAL_LANG, if it has not been initialized | |
* necessary if called by command controller | |
*/ | |
self::initializeLocalization($extensionName); | |
if ($languageKey != '') { | |
self::$languageKey = $languageKey; | |
if (isset(self::$LOCAL_LANG[$extensionName][$languageKey]) === false) { | |
self::addLocalLanguage($languageKey, $extensionName); | |
} | |
} | |
} | |
/** | |
* Check if language is set in self::$LOCAL_LANG | |
* @param string $languageKey | |
* @param string $extensionName | |
* @return bool | |
*/ | |
public static function issetLanguage($languageKey = '', $extensionName = 'example') | |
{ | |
return isset(self::$LOCAL_LANG[$extensionName][$languageKey]); | |
} | |
/** | |
* Quite the same as initializeLocalization without self::setLanguageKeys(); | |
* Loads local-language values by looking for a "locallang.xlf" (or "locallang.xml") file in the plugin resources directory and if found includes it. | |
* Also locallang values set in the TypoScript property "_LOCAL_LANG" are merged onto the values found in the "locallang.xlf" file. | |
* | |
* @param $languageKey | |
* @param string $extensionName | |
*/ | |
private static function addLocalLanguage($languageKey, $extensionName = 'example') | |
{ | |
if (isset(self::$LOCAL_LANG[$extensionName][$languageKey])) { | |
return; | |
} | |
$locallangPathAndFilename = 'EXT:' . GeneralUtility::camelCaseToLowerCaseUnderscored($extensionName) . '/' . self::$locallangPath . 'locallang.xlf'; | |
self::setLanguageKeys(); | |
$renderCharset = TYPO3_MODE === 'FE' ? self::getTypoScriptFrontendController()->renderCharset : self::getLanguageService()->charSet; | |
/** @var $languageFactory LocalizationFactory */ | |
$languageFactory = GeneralUtility::makeInstance(LocalizationFactory::class); | |
self::$LOCAL_LANG[$extensionName] = $languageFactory->getParsedData($locallangPathAndFilename, self::$languageKey, $renderCharset); | |
$tempLL = $languageFactory->getParsedData($locallangPathAndFilename, $languageKey, $renderCharset); | |
if (isset($tempLL[$languageKey])) { | |
self::$LOCAL_LANG[$extensionName][$languageKey] = $tempLL[$languageKey]; | |
} | |
self::loadTypoScriptLabels($extensionName); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment