Skip to content

Commit

Permalink
Create cleanup-empty-translations.php
Browse files Browse the repository at this point in the history
  • Loading branch information
adrolli authored Jan 19, 2025
1 parent c184b94 commit cceb1a0
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions .github/scripts/cleanup-empty-translations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/usr/bin/env php
<?php

/**
* This script scans your monorepo for PHP translation files and removes
* empty-string keys. If the resulting array is empty, the file is deleted.
*
* Adjust paths if your directory structure differs.
*/

// 1. Use a glob to gather all translation PHP files in all packages:
$pattern = __DIR__ . '/../../packages/*/resources/lang/*/*.php';
$phpFiles = glob($pattern);

foreach ($phpFiles as $phpFilePath) {
cleanupPhpTranslationFile($phpFilePath);
}

/**
* Loads the translation file as an array, removes empty-string values,
* and deletes the file if the array ends up empty.
*/
function cleanupPhpTranslationFile(string $filePath): void
{
// Attempt to load the file's return array:
$translations = (static function ($file) {
return include $file;
})($filePath);

// If the file doesn't return an array, skip it
if (! is_array($translations)) {
return;
}

// Filter out keys whose value is exactly '' (empty string)
$filtered = array_filter($translations, fn($value) => $value !== '');

// If there's nothing left, delete the file
if (empty($filtered)) {
echo "Deleting completely empty translation file: $filePath\n";
unlink($filePath);
return;
}

// If changes were made (some keys removed), rewrite the file
if ($filtered !== $translations) {
rewritePhpTranslationFile($filePath, $filtered);
}
}

/**
* Overwrites the PHP file with a cleaned translation array.
*/
function rewritePhpTranslationFile(string $filePath, array $translations): void
{
$exported = var_export($translations, true);
$phpFileContent = "<?php\n\nreturn {$exported};\n";
file_put_contents($filePath, $phpFileContent);
echo "Cleaned empty translations in: $filePath\n";
}

0 comments on commit cceb1a0

Please sign in to comment.