From 155066be130b42de199ecefaaec3f2e47c87822b Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Mon, 19 Aug 2024 16:48:03 +0200 Subject: [PATCH] Remove irrelevant command --- .../DetectMissingTranslationsCommand.php | 199 ------------------ 1 file changed, 199 deletions(-) delete mode 100644 src/Bundle/ChillMainBundle/Command/DetectMissingTranslationsCommand.php diff --git a/src/Bundle/ChillMainBundle/Command/DetectMissingTranslationsCommand.php b/src/Bundle/ChillMainBundle/Command/DetectMissingTranslationsCommand.php deleted file mode 100644 index 12a194699..000000000 --- a/src/Bundle/ChillMainBundle/Command/DetectMissingTranslationsCommand.php +++ /dev/null @@ -1,199 +0,0 @@ -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("The locale file '$localeFile' does not exist."); - 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("No missing translations found."); - } else { - $output->writeln("Missing translations:"); - foreach ($missingKeys as $key => $info) { - $output->writeln("$key: $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; - } -}