|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace JsonTranslationHelper\Command; |
| 4 | + |
| 5 | +use Illuminate\Console\Command; |
| 6 | + |
| 7 | +class TranslationHelperCommand extends Command |
| 8 | +{ |
| 9 | + /** |
| 10 | + * @var string |
| 11 | + */ |
| 12 | + protected $signature = 'translation:scan'; |
| 13 | + |
| 14 | + /** |
| 15 | + * @var string |
| 16 | + */ |
| 17 | + protected $description = 'Searches for translation keys – inserts into JSON translation files.'; |
| 18 | + |
| 19 | + /** |
| 20 | + * Execute the console command. |
| 21 | + * |
| 22 | + * @return void |
| 23 | + */ |
| 24 | + public function handle() |
| 25 | + { |
| 26 | + $translationKeys = $this->findProjectTranslationsKeys(); |
| 27 | + $translationFiles = $this->getProjectTranslationFiles(); |
| 28 | + |
| 29 | + foreach ($translationFiles as $file) { |
| 30 | + $translationData = $this->getAlreadyTranslatedKeys($file); |
| 31 | + $this->line("lang " . str_replace('.json', '', basename($file))); |
| 32 | + $added = []; |
| 33 | + |
| 34 | + foreach ($translationKeys as $key) { |
| 35 | + if (!isset($translationData[$key])) { |
| 36 | + $this->warn(" - Added {$key}"); |
| 37 | + $translationData[$key] = ''; |
| 38 | + $added[] = $key; |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + if ($added) { |
| 43 | + $this->line("Updating translation file..."); |
| 44 | + $this->writeNewTranslationFile($file, $translationData); |
| 45 | + $this->info("Done!"); |
| 46 | + } else { |
| 47 | + $this->warn("Nothing new found for this language."); |
| 48 | + } |
| 49 | + $this->line(""); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @return array |
| 55 | + */ |
| 56 | + private function findProjectTranslationsKeys() |
| 57 | + { |
| 58 | + $allKeys = []; |
| 59 | + $viewsDirectories = config('translation-helper.views_directories'); |
| 60 | + $fileExtensions = config('translation-helper.file_extensions'); |
| 61 | + |
| 62 | + foreach($viewsDirectories as $directory) { |
| 63 | + foreach ($fileExtensions as $extension) { |
| 64 | + $this->getTranslationKeysFromDir($allKeys, $directory, $extension); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + ksort($allKeys); |
| 69 | + |
| 70 | + return $allKeys; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @param array $keys |
| 75 | + * @param string $dirPath |
| 76 | + * @param string $fileExt |
| 77 | + */ |
| 78 | + private function getTranslationKeysFromDir(&$keys, $dirPath, $fileExt = 'php') |
| 79 | + { |
| 80 | + $files = glob_recursive("{$dirPath}/*.{$fileExt}", GLOB_BRACE); |
| 81 | + |
| 82 | + foreach ($files as $file) { |
| 83 | + $content = $this->getSanitizedContent($file); |
| 84 | + |
| 85 | + $this->getTranslationKeysFromFunction($keys, 'lang', $content); |
| 86 | + $this->getTranslationKeysFromFunction($keys, '__', $content); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * @param array $keys |
| 92 | + * @param string $functionName |
| 93 | + * @param string $content |
| 94 | + */ |
| 95 | + private function getTranslationKeysFromFunction(&$keys, $functionName, $content) |
| 96 | + { |
| 97 | + $matches = []; |
| 98 | + |
| 99 | + preg_match_all("#{$functionName}\(\'(.*?)\'\)#", $content, $matches); |
| 100 | + |
| 101 | + if ( ! empty($matches)) { |
| 102 | + foreach ($matches[1] as $match) { |
| 103 | + $match = str_replace('"', "'", str_replace("\'", "'", $match)); |
| 104 | + |
| 105 | + if ( ! empty($match)) { |
| 106 | + $keys[$match] = $match; |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * @return array |
| 114 | + */ |
| 115 | + private function getProjectTranslationFiles() |
| 116 | + { |
| 117 | + $path = config('translation-helper.translations_output'); |
| 118 | + $files = glob("{$path}/*.json", GLOB_BRACE); |
| 119 | + |
| 120 | + return $files; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * @param string $filePath |
| 125 | + * @return array |
| 126 | + */ |
| 127 | + private function getAlreadyTranslatedKeys($filePath) |
| 128 | + { |
| 129 | + $current = json_decode(file_get_contents($filePath), true); |
| 130 | + |
| 131 | + ksort($current); |
| 132 | + |
| 133 | + return $current; |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * @param string $filePath |
| 138 | + * @param array $translations |
| 139 | + */ |
| 140 | + private function writeNewTranslationFile($filePath, $translations) |
| 141 | + { |
| 142 | + file_put_contents($filePath, json_encode($translations, JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE)); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * @param string $filePath |
| 147 | + * @return string |
| 148 | + */ |
| 149 | + private function getSanitizedContent($filePath) |
| 150 | + { |
| 151 | + return str_replace("\n", ' ', file_get_contents($filePath)); |
| 152 | + } |
| 153 | +} |
0 commit comments