-
-
Save astehlik/a08d79930e612a11b84b to your computer and use it in GitHub Desktop.
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 Tx\Myutils\Utility; | |
/* * | |
* This script belongs to the TYPO3 Extension "myutils". * | |
* * | |
* It is free software; you can redistribute it and/or modify it under * | |
* the terms of the GNU General Public License, either version 3 of the * | |
* License, or (at your option) any later version. * | |
* * | |
* The TYPO3 project - inspiring people to share! * | |
* */ | |
use TYPO3\CMS\Core\SingletonInterface; | |
/** | |
* This class contains utility functions for language file handling. | |
*/ | |
class LanguageUtility implements SingletonInterface { | |
/** | |
* Following snippet lets you easily override XLIFF-based localization files for any extension. | |
* | |
* Create localization files within your extension in: | |
* | |
* Resources/Private/Language/Overrides/<extension-key>.<original-name>.xlf | |
* Resources/Private/Language/Overrides/<locale>.<extension-key>.<original-name>.xlf | |
* | |
* E.g., you want to override EXT:news/Resources/Private/Language/locallang_db.xlf, then create | |
* | |
* Resources/Private/Language/Overrides/news.locallang_db.xlf | |
* | |
* and, to override the French localization: | |
* | |
* Resources/Private/Language/Overrides/fr.news.locallang_db.xlf | |
* | |
* BEWARE: In order for French (or any other language) override to work, you MUST have the original | |
* French localization for the corresponding extension. | |
* | |
* Call this method in a ext_tables.php file. | |
* | |
* @see https://gist.github.com/xperseguers/99163ef8b01edb8f5333 | |
* @author Xavier Perseguers xperseguers | |
*/ | |
public function overrideLanguageFiles() { | |
$languageOverridePath = \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('myutils') . 'Resources/Private/Language/Overrides/'; | |
$extensionRelativePath = substr($languageOverridePath, strlen(PATH_site)); | |
$languageFiles = \TYPO3\CMS\Core\Utility\GeneralUtility::getFilesInDir($languageOverridePath, 'xlf'); | |
foreach ($languageFiles as $file) { | |
$parts = explode('.', $file); | |
if (count($parts) === 3) { | |
$locale = 'default'; | |
$extensionKey = $parts[0]; | |
$languageFileWithoutExtension = $parts[1]; | |
} elseif (count($parts) === 4) { | |
$locale = $parts[0]; | |
$extensionKey = $parts[1]; | |
$languageFileWithoutExtension = $parts[2]; | |
} else { | |
throw new \RuntimeException('The file located at ' . $languageOverridePath . $file . ' can not be used for overriding a language file. Please see LangaugeUtility::overrideLanguageFiles() for details.'); | |
continue; | |
} | |
$originalLanguageFileName = $this->detectOriginalLanguageFile($extensionKey, $languageFileWithoutExtension); | |
if (!isset($originalLanguageFileName)) { | |
throw new \RuntimeException('Original file for Override ' . $file . ' could not be found.'); | |
} | |
// We need to add the absolute file to the override configuration as well because | |
// some extension already to a getFileAbsFileName() before calling the localization | |
// utility. In that case the "EXT:" prefixed version will not work for the override. | |
$originalLanguageFileNameAbsolute = \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName($originalLanguageFileName); | |
if ($locale === 'default') { | |
// Register overlay for English localization file | |
$GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'][$originalLanguageFileName][] = $extensionRelativePath . $file; | |
$GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'][$originalLanguageFileNameAbsolute][] = $extensionRelativePath . $file; | |
} else { | |
// Register overlay for a given locale localization file | |
$GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'][$locale][$originalLanguageFileName][] = $extensionRelativePath . $file; | |
$GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'][$locale][$originalLanguageFileNameAbsolute][] = $extensionRelativePath . $file; | |
} | |
} | |
} | |
/** | |
* Tries to detect the original language file by looping over all possible Extensions. | |
* | |
* @param string $extensionKey | |
* @param string $languageFileWithoutExtension | |
* @return string | |
*/ | |
protected function detectOriginalLanguageFile($extensionKey, $languageFileWithoutExtension) { | |
$originalLanguageFileName = NULL; | |
foreach (array('.xlf', '.xml') as $possibleExtension) { | |
$possibleOriginalFile = 'EXT:' . $extensionKey . '/Resources/Private/Language/' . $languageFileWithoutExtension . $possibleExtension; | |
$possibleOriginalFileAbsolute = \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName($possibleOriginalFile); | |
if (is_file($possibleOriginalFileAbsolute)) { | |
$originalLanguageFileName = $possibleOriginalFile; | |
break; | |
} | |
} | |
if (!isset($originalLanguageFileName) && $extensionKey === 'lang' && file_exists(\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('lang') . $languageFileWithoutExtension . '.xlf')) { | |
$originalLanguageFileName = 'EXT:' . $extensionKey . '/' . $languageFileWithoutExtension . '.xlf'; | |
} | |
return $originalLanguageFileName; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment