Skip to content

Instantly share code, notes, and snippets.

@xperseguers
Last active April 26, 2024 19:33
Show Gist options
  • Save xperseguers/99163ef8b01edb8f5333 to your computer and use it in GitHub Desktop.
Save xperseguers/99163ef8b01edb8f5333 to your computer and use it in GitHub Desktop.
Generic override XLIFF in TYPO3 CMS
<?php
/**
* 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.
*/
$languageOverridePath = \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath($_EXTKEY) . '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];
$languageFile = $parts[1] . '.' . $parts[2];
} elseif (count($parts) === 4) {
$locale = $parts[0];
$extensionKey = $parts[1];
$languageFile = $parts[2] . '.' . $parts[3];
} else {
// Unsupported localization file
continue;
}
$originalLanguageFileName = 'EXT:' . $extensionKey . '/Resources/Private/Language/' . $languageFile;
if ($locale === 'default') {
// Register overlay for English localization file
$GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'][$originalLanguageFileName][] = $extensionRelativePath . $file;
} else {
// Register overlay for a given locale localization file
$GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'][$locale][$originalLanguageFileName][] = $extensionRelativePath . $file;
}
}
@astehlik
Copy link

astehlik commented Dec 4, 2014

Hi Xavier,

thank you for this practical snippet! I have one suggestion for improvement.

Some Extensions seem to pass the absolute path to the localization utility. Then the override will not work.

This is why I resolve the absolute path to the original file and add it to the override configuration as well. You can have a look at my version at:

https://gist.github.com/astehlik/a08d79930e612a11b84b

Cheers,
Alex

@astehlik
Copy link

Hi Xavier,

I have one additional remark: I realized that you named the Gist file ext_tables.php but the comment in the code says ext_localconf.php.

Useing ext_localconf.php is problematic because then you might run into trouble when you want to override translations of the context_help extension. It makes use of the override mechanism (for including the files from the 4.5 folder). But there the ext_tables.php file is used (which seems to be loaded after ext_tables.php) and therefore your overrides will be ignored.

Cheers,
Alex

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment