Skip to content

Instantly share code, notes, and snippets.

@trbsi
Last active December 26, 2023 11:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trbsi/0dfd3c23cba1c613da114e44610efe8e to your computer and use it in GitHub Desktop.
Save trbsi/0dfd3c23cba1c613da114e44610efe8e to your computer and use it in GitHub Desktop.
LARAVEL. Scripts goes through all .php and .blade.php files and looks for missing translation keys and adds it to the en.json file. If there are some non-used translations in en.json file they will be deleted.
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use RecursiveRegexIterator;
use RegexIterator;
class ExtractTranslationsCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:extract-translations-command';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Extract translations from files and add it to .json file';
private string $phpDir;
private string $bladeDir;
private string $jsonFile;
private int $numberOfRemovedTranslations = 0;
private int $numberOfAddedTranslations = 0;
public function handle()
{
$this->phpDir = base_path() . '/app';
$this->bladeDir = base_path() . '/resources/views';
$this->jsonFile = base_path() . '/lang/en.json';
$phpFiles = $this->getFiles($this->phpDir);
$bladeFiles = $this->getFiles($this->bladeDir);
$phpStrings = $this->extractStringsFromFiles($phpFiles);
$bladeStrings = $this->extractStringsFromFiles($bladeFiles);
$allStrings = array_merge($phpStrings, $bladeStrings);
$this->insertIntoJson($allStrings, $this->jsonFile);
$this->removeFromJson($allStrings, $this->jsonFile);
$this->info("Strings inserted into {$this->jsonFile} successfully.");
$this->info('Number of added translations: ' . $this->numberOfAddedTranslations);
$this->info('Number of removed translations: ' . $this->numberOfRemovedTranslations);
}
private function getFiles($dir)
{
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
$regexIterator = new RegexIterator(
$iterator, '/^.+\.php$|^.+\.blade\.php$/i', RecursiveRegexIterator::GET_MATCH
);
$files = [];
foreach ($regexIterator as $file) {
$files[] = $file[0];
}
return $files;
}
private function extractStringsFromFiles($files)
{
$strings = [];
$patterns = ["/__\('(.*?)'\)/", "/trans_choice\('([^']+)',/"];
foreach ($patterns as $pattern) {
foreach ($files as $file) {
$content = file_get_contents($file);
if (preg_match_all($pattern, $content, $matches)) {
$strings = array_merge($strings, $matches[1]);
}
}
}
return $strings;
}
private function insertIntoJson(array $strings, string $jsonFile)
{
$existingData = [];
if (file_exists($jsonFile)) {
$existingData = json_decode(file_get_contents($jsonFile), true);
}
foreach ($strings as $string) {
if (!array_key_exists($string, $existingData)) {
$existingData[$string] = $string;
$this->numberOfAddedTranslations++;
}
}
ksort($existingData);
file_put_contents($jsonFile, json_encode($existingData, JSON_PRETTY_PRINT));
}
private function removeFromJson(array $strings, string $jsonFile): void
{
$existingData = [];
if (file_exists($jsonFile)) {
$existingData = json_decode(file_get_contents($jsonFile), true);
}
foreach ($existingData as $stringKey => $stringValue) {
if (!in_array($stringKey, $strings)) {
unset($existingData[$stringKey]);
$this->numberOfRemovedTranslations++;
}
}
ksort($existingData);
file_put_contents($jsonFile, json_encode($existingData, JSON_PRETTY_PRINT));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment