/** * 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); }
/** * Get an array of keys that have missing values with a hint * from another language translation file if possible. * * ex: [ ['key' => 'product.color.nl', 'hint' => 'en = "color"'] ] * * @param array $languages * @return array */ private function getMissing(array $languages) { $files = $this->manager->files(); // Array of content of all files indexed by fileName.languageKey $filesResults = []; // The final output of the method $missing = []; // Here we collect the file results foreach ($files as $fileName => $languageFiles) { foreach ($languageFiles as $languageKey => $filePath) { $filesResults[$fileName][$languageKey] = $this->manager->getFileContent($filePath); } } $values = Arr::dot($filesResults); $emptyValues = array_filter($values, function ($value) { return $value == ''; }); // Adding all keys that has values = '' foreach ($emptyValues as $dottedValue => $emptyValue) { list($fileName, $languageKey, $key) = explode('.', $dottedValue, 3); $missing[] = "{$fileName}.{$key}:{$languageKey}"; } $missing = array_merge($missing, $this->manager->getKeysExistingInALanguageButNotTheOther($values)); return $missing; }
/** * Collect translation values from console via questions. * * @param $languages * @return array */ private function collectValues($languages) { $values = []; foreach ($languages as $languageKey) { $languageContent = $this->manager->getFileContent($this->files[$languageKey]); $values[$languageKey] = $this->ask(sprintf('<fg=yellow>%s.%s:%s</> translation', $this->fileName, $this->key, $languageKey), isset($languageContent[$this->key]) ? $languageContent[$this->key] : null); } return $values; }
/** * 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; }
/** * Get an array of application files containing the old key. * * @return array */ private function getFilesContainingOldKey() { $affectedFiles = []; foreach ($this->manager->getAllViewFilesWithTranslations() as $file => $keys) { foreach ($keys as $key) { if ($key == $this->argument('oldKey')) { $affectedFiles[$file][] = $key; } } } return $affectedFiles; }
/** * Execute the console command. * * @return mixed */ public function handle() { try { list($file, $key) = explode('.', $this->argument('key'), 2); } catch (\ErrorException $e) { $this->error('Could not recognize the key you want to remove.'); return; } if ($this->confirm("Are you sure you want to remove \"{$file}.{$key}\"?")) { if (Str::contains($file, '::')) { try { $parts = explode('::', $file); $this->manager->setPathToVendorPackage($parts[0]); $file = $parts[1]; } catch (\ErrorException $e) { $this->error('Could not recognize the package.'); return; } } $this->manager->removeKey($file, $key); $this->info("{$file}.{$key} was removed successfully."); } }
/** * Synchronize keys that exist in a language but not the other. * * @param $translationFiles * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException * @return void */ private function syncKeysBetweenLanguages($translationFiles) { $this->info('Synchronizing language files...'); $filesResults = []; // Here we collect the file results foreach ($translationFiles as $fileName => $languageFiles) { foreach ($languageFiles as $languageKey => $filePath) { $filesResults[$fileName][$languageKey] = $this->manager->getFileContent($filePath); } } $values = Arr::dot($filesResults); $missing = $this->manager->getKeysExistingInALanguageButNotTheOther($values); foreach ($missing as &$missingKey) { list($file, $key) = explode('.', $missingKey, 2); list($key, $language) = explode(':', $key, 2); $this->fillMissingKeys($file, [$key], $language); } }