mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-12 21:34:25 +00:00
Remove irrelevant command
This commit is contained in:
parent
a5329c5d69
commit
155066be13
@ -1,199 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\MainBundle\Command;
|
||||
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
use Symfony\Component\Filesystem\Filesystem;
|
||||
|
||||
class DetectMissingTranslationsCommand extends Command
|
||||
{
|
||||
protected static $defaultName = 'chill:main:detect-missing-translations';
|
||||
|
||||
private const BASE_DIR = 'vendor/chill-project/chill-bundles/src/Bundle';
|
||||
|
||||
protected function configure(): void
|
||||
{
|
||||
$this
|
||||
->setDescription('Checks for missing translations in the specified locale.')
|
||||
->addOption('bundle', null, InputOption::VALUE_REQUIRED, 'The relative path to the translation files', 'translations')
|
||||
->addOption('locale', null, InputOption::VALUE_REQUIRED, 'The locale to check for missing translations', 'fr');
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
$locale = $input->getOption('locale');
|
||||
$bundle = $input->getOption('bundle');
|
||||
$bundlePath = self::BASE_DIR . '/' . $bundle;
|
||||
// $vuePath = $bundlePath . '/Resources/public/vuejs';
|
||||
$twigPath = $bundlePath . '/Resources/public';
|
||||
$localeFile = $bundlePath . "/translations/messages.$locale.yml";
|
||||
|
||||
if (!file_exists($localeFile)) {
|
||||
$output->writeln("<error>The locale file '$localeFile' does not exist.</error>");
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
$missingKeys = [];
|
||||
|
||||
// Load existing translations for the specified locale
|
||||
$existingTranslations = Yaml::parseFile($localeFile);
|
||||
|
||||
// Extract translation keys from Vue components
|
||||
/* $vueKeys = $this->extractVueKeys($vuePath);
|
||||
foreach ($vueKeys as $key) {
|
||||
if (!$this->keyExistsInTranslations($key, $existingTranslations)) {
|
||||
$missingKeys[$key] = 'Missing translation in Vue component';
|
||||
}
|
||||
}*/
|
||||
|
||||
// Extract translation keys from Twig templates
|
||||
$twigKeys = $this->extractTwigKeys($twigPath);
|
||||
foreach ($twigKeys as $key) {
|
||||
if (!$this->keyExistsInTranslations($key, $existingTranslations)) {
|
||||
$missingKeys[$key] = 'Missing translation in Twig template';
|
||||
}
|
||||
}
|
||||
|
||||
// Extract translation keys from PHP code
|
||||
$phpKeys = $this->extractPhpKeys($bundlePath);
|
||||
foreach ($phpKeys as $key) {
|
||||
if (!$this->keyExistsInTranslations($key, $existingTranslations)) {
|
||||
$missingKeys[$key] = 'Missing translation in PHP code';
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($missingKeys)) {
|
||||
$output->writeln("<info>No missing translations found.</info>");
|
||||
} else {
|
||||
$output->writeln("<comment>Missing translations:</comment>");
|
||||
foreach ($missingKeys as $key => $info) {
|
||||
$output->writeln("<info>$key:</info> $info");
|
||||
}
|
||||
|
||||
// Prompt user to add missing translations
|
||||
$this->promptForTranslations($missingKeys, $localeFile);
|
||||
}
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
private function extractVueKeys($path)
|
||||
{
|
||||
$keys = [];
|
||||
$files = $this->getAllFiles($path, ['vue']);
|
||||
|
||||
foreach ($files as $file) {
|
||||
$content = file_get_contents($file);
|
||||
preg_match_all('/\$t\((\'|")(.*?)(\'|")\)/', $content, $matches);
|
||||
foreach ($matches[2] as $match) {
|
||||
$keys[] = $match;
|
||||
}
|
||||
}
|
||||
|
||||
return array_unique($keys);
|
||||
}
|
||||
|
||||
private function extractTwigKeys($path)
|
||||
{
|
||||
$keys = [];
|
||||
$files = $this->getAllFiles($path, ['html.twig', 'twig']);
|
||||
|
||||
foreach ($files as $file) {
|
||||
$content = file_get_contents($file);
|
||||
preg_match_all('/\|trans\s*\(\'(.*?)\'/', $content, $matches);
|
||||
foreach ($matches[1] as $match) {
|
||||
$keys[] = $match;
|
||||
}
|
||||
}
|
||||
|
||||
return array_unique($keys);
|
||||
}
|
||||
|
||||
private function extractPhpKeys($path)
|
||||
{
|
||||
$keys = [];
|
||||
$files = $this->getAllFiles($path, ['php']);
|
||||
|
||||
foreach ($files as $file) {
|
||||
$content = file_get_contents($file);
|
||||
preg_match_all('/->trans\((\'|")(.*?)(\'|")\)/', $content, $matches);
|
||||
foreach ($matches[2] as $match) {
|
||||
$keys[] = $match;
|
||||
}
|
||||
}
|
||||
|
||||
return array_unique($keys);
|
||||
}
|
||||
|
||||
private function getAllFiles($directory, array $extensions)
|
||||
{
|
||||
$files = [];
|
||||
$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($directory));
|
||||
|
||||
foreach ($iterator as $file) {
|
||||
if ($file->isFile() && in_array($file->getExtension(), $extensions)) {
|
||||
$files[] = $file->getPathname();
|
||||
}
|
||||
}
|
||||
|
||||
return $files;
|
||||
}
|
||||
|
||||
private function keyExistsInTranslations($key, $translations)
|
||||
{
|
||||
$keys = explode('.', $key);
|
||||
$current = $translations;
|
||||
|
||||
foreach ($keys as $part) {
|
||||
if (isset($current[$part])) {
|
||||
$current = $current[$part];
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private function promptForTranslations($missingKeys, $localeFile)
|
||||
{
|
||||
$fs = new Filesystem();
|
||||
foreach ($missingKeys as $key => $info) {
|
||||
$translation = readline("Enter translation for '$key': ");
|
||||
if ($translation !== '') {
|
||||
// Add translation to the YAML file
|
||||
$existing = file_exists($localeFile) ? Yaml::parseFile($localeFile) : [];
|
||||
$this->addTranslation($existing, $key, $translation);
|
||||
$fs->dumpFile($localeFile, Yaml::dump($existing));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function addTranslation(&$translations, $key, $translation)
|
||||
{
|
||||
$keys = explode('.', $key);
|
||||
$current = &$translations;
|
||||
|
||||
foreach ($keys as $part) {
|
||||
if (!isset($current[$part])) {
|
||||
$current[$part] = [];
|
||||
}
|
||||
$current = &$current[$part];
|
||||
}
|
||||
|
||||
$current = $translation;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user