/** * 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); }
/** * The output of the table rows. * * @return array */ private function tableRows() { $output = []; $filesContent = []; foreach ($this->files as $languageKey => $file) { foreach ($filesContent[$languageKey] = Arr::dot($this->manager->getFileContent($file)) as $key => $value) { if (!$this->shouldShowKey($key)) { continue; } $output[$key]['key'] = $key; $output[$key][$languageKey] = $value ?: ''; } } // Now that we have collected all existing values, we are going to fill the // missing ones with emptiness indicators to balance the table structure // and alert developers so that they can take proper actions. foreach ($output as $key => $values) { $original = []; foreach ($this->languages as $languageKey) { $original[$languageKey] = isset($values[$languageKey]) && $values[$languageKey] ? $values[$languageKey] : '<bg=red> MISSING </>'; } // Sort the language values based on language name ksort($original); $output[$key] = array_merge(['key' => "<fg=yellow>{$key}</>"], $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; }
/** * Rename the given oldKey to the newKey. * * @return void */ private function renameKey() { try { list($file, $key) = explode('.', $this->argument('oldKey'), 2); } catch (\ErrorException $e) { $this->error('Could not recognize the key you want to rename.'); return; } if (Str::contains($this->argument('newKey'), '.')) { $this->error('Please provide the new key must not contain a dot.'); return; } $newKey = preg_replace('/(\\w+)$/i', $this->argument('newKey'), $key); $files = $this->manager->files()[$file]; $currentValues = []; foreach ($files as $languageKey => $filePath) { $content = Arr::dot($this->manager->getFileContent($filePath)); $currentValues[$languageKey] = isset($content[$key]) ? $content[$key] : ''; } $this->manager->removeKey($file, $key); $this->manager->fillKeys($file, [$newKey => $currentValues]); }
/** * 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); } }