new command to delete empty translations (#3392)
* composer update and phpstan issues * bump version
This commit is contained in:
@@ -51,6 +51,7 @@ class TranslationCommand extends Command
|
||||
->addOption('delete-resname', null, InputOption::VALUE_REQUIRED, 'Deletes the translation by resname')
|
||||
->addOption('extension', null, InputOption::VALUE_NONE, 'Find translation files with wrong extensions')
|
||||
->addOption('fill-empty', null, InputOption::VALUE_NONE, 'Pre-fills empty translations with the english version')
|
||||
->addOption('delete-empty', null, InputOption::VALUE_NONE, 'Delete all empty kyes and files which have no translated key at all')
|
||||
// DEEPL TRANSLATION FEATURE - UNTESTED
|
||||
->addOption('translate-locale', null, InputOption::VALUE_REQUIRED, 'Translate into the given locale with Deepl')
|
||||
// @see https://www.deepl.com/de/pro#developer
|
||||
@@ -131,7 +132,7 @@ class TranslationCommand extends Command
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->fixEmptyTranslations($file, $translations[$fromLocale][$name]);
|
||||
$this->fillEmptyTranslations($file, $translations[$fromLocale][$name]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -179,6 +180,17 @@ class TranslationCommand extends Command
|
||||
}
|
||||
}
|
||||
|
||||
// ==========================================================================
|
||||
// Delete empty translation keys and files without any translation
|
||||
// ==========================================================================
|
||||
if ($input->getOption('delete-empty')) {
|
||||
foreach ($bases as $directory) {
|
||||
foreach (glob($directory) as $file) {
|
||||
$this->removeEmptyTranslations($io, $file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ==========================================================================
|
||||
// DEEPL
|
||||
// ==========================================================================
|
||||
@@ -399,7 +411,7 @@ class TranslationCommand extends Command
|
||||
file_put_contents($file, $xmlDocument->saveXML());
|
||||
}
|
||||
|
||||
private function fixEmptyTranslations(string $file, array $translations): void
|
||||
private function fillEmptyTranslations(string $file, array $translations): void
|
||||
{
|
||||
$xml = simplexml_load_file($file);
|
||||
$foundEmpty = false;
|
||||
@@ -463,6 +475,51 @@ class TranslationCommand extends Command
|
||||
file_put_contents($file, $xmlDocument->saveXML());
|
||||
}
|
||||
|
||||
private function removeEmptyTranslations(SymfonyStyle $io, string $file): void
|
||||
{
|
||||
$xml = simplexml_load_file($file);
|
||||
$hasTranslation = false;
|
||||
|
||||
/** @var \SimpleXMLElement $unit */
|
||||
foreach ($xml->file->body->{'trans-unit'} as $unit) {
|
||||
$translation = (string) $unit->target;
|
||||
if (\strlen($translation) > 0) {
|
||||
$hasTranslation = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$hasTranslation) {
|
||||
unlink($file);
|
||||
$io->warning('Removed empty translation file: ' . basename($file));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$xmlDocument = new \DOMDocument('1.0');
|
||||
$xmlDocument->preserveWhiteSpace = false;
|
||||
$xmlDocument->loadXML($xml->asXML());
|
||||
|
||||
$removedTranslation = false;
|
||||
$elements = $xmlDocument->getElementsByTagName('target');
|
||||
foreach ($elements as $element) {
|
||||
if ($element->nodeValue === '' || $element->nodeValue === null) {
|
||||
/** @var \DOMElement $parent */
|
||||
$parent = $element->parentNode;
|
||||
$io->text('Remove empty translation: ' . $parent->getAttribute('resname'));
|
||||
$parent->parentNode->removeChild($parent);
|
||||
$removedTranslation = true;
|
||||
}
|
||||
}
|
||||
|
||||
if ($removedTranslation) {
|
||||
$xmlDocument->formatOutput = true;
|
||||
|
||||
file_put_contents($file, $xmlDocument->saveXML());
|
||||
$io->warning('Removed empty translations from file: ' . basename($file));
|
||||
}
|
||||
}
|
||||
|
||||
private function generateId(string $source): string
|
||||
{
|
||||
return strtr(substr(base64_encode(hash('sha256', $source, true)), 0, 7), '/+', '._');
|
||||
|
||||
Reference in New Issue
Block a user