/** * The output of the table rows. * * @return array */ private function tableRows() { $allLanguages = $this->manager->languages(); $filesContent = []; $output = []; foreach ($this->files as $fileName => $fileLanguages) { foreach ($fileLanguages as $languageKey => $filePath) { $lines = $filesContent[$fileName][$languageKey] = Arr::dot($this->manager->getFileContent($filePath)); foreach ($lines as $key => $line) { if (!is_array($line) && stripos($line, $this->argument('keyword')) !== false) { $output[$fileName . '.' . $key][$languageKey] = "<bg=yellow;fg=black>{$line}</>"; } } } } // Now that we collected all values that matches the keyword argument // in a close match, we collect the values for the rest of the // languages for the found keys to complete the table view. foreach ($output as $fullKey => $values) { list($fileName, $key) = explode('.', $fullKey, 2); $original = []; foreach ($allLanguages as $languageKey) { $original[$languageKey] = isset($values[$languageKey]) ? $values[$languageKey] : isset($filesContent[$fileName][$languageKey][$key]) ? $filesContent[$fileName][$languageKey][$key] : ''; } // Sort the language values based on language name ksort($original); $output[$fullKey] = array_merge(['key' => "<fg=yellow>{$fullKey}</>"], $original); } return array_values($output); }
/** * Execute the console command. * * @return mixed */ public function handle() { $this->info('Looking for missing translations...'); $languages = $this->manager->languages(); $missing = $this->getMissing($languages); $values = $this->collectValues($missing); $input = []; foreach ($values as $key => $value) { preg_match('/^([^\\.]*)\\.(.*):(.*)/', $key, $matches); $input[$matches[1]][$matches[2]][$matches[3]] = $value; $this->line("\"<fg=yellow>{$key}</>\" was set to \"<fg=yellow>{$value}</>\" successfully."); } foreach ($input as $fileName => $values) { $this->manager->fillKeys($fileName, $values); } $this->info('Done!'); }
/** * Get the languages to be displayed in the command output. * * @return array */ private function getLanguages() { $allLanguages = $this->manager->languages(); if (!$this->option('lang')) { return $allLanguages; } $userLanguages = explode(',', (string) $this->option('lang')); if ($missingLanguages = array_diff($userLanguages, $allLanguages)) { throw new InvalidArgumentException('Unknown Language(s) [' . implode(',', $missingLanguages) . '].'); } return $userLanguages; }
/** * Fill a translation key in all languages. * * @return void */ private function fillKey() { $languages = $this->manager->languages(); if ($this->languageKey) { if (!in_array($this->languageKey, $languages)) { $this->error(sprintf('Language (%s) could not be found!', $this->languageKey)); return; } // If a language key was specified then we prompt for it only. $languages = [$this->languageKey]; } $values = $this->collectValues($languages); $this->manager->fillKeys(str_replace($this->packageName . '::', '', $this->fileName), [$this->key => $values]); foreach ($values as $languageKey => $value) { $this->line("<fg=yellow>{$this->fileName}.{$this->key}:{$languageKey}</> was set to \"<fg=yellow>{$value}</>\" successfully."); } }