From 665cd131072d6be570c2c2e29d29e5f29e9e30e8 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Thu, 22 Feb 2024 01:05:22 +0300 Subject: [PATCH] Fix for fix :) --- src/Helpers/Arr.php | 13 +++++++++++ src/Processors/Processor.php | 43 +++++++++++++++++++---------------- src/Resources/Translation.php | 32 ++++++++++---------------- 3 files changed, 49 insertions(+), 39 deletions(-) diff --git a/src/Helpers/Arr.php b/src/Helpers/Arr.php index 6c3bdad6..b98dde38 100644 --- a/src/Helpers/Arr.php +++ b/src/Helpers/Arr.php @@ -36,4 +36,17 @@ protected function filter(array $source, array $target, bool $filter_keys = fals { return $filter_keys ? DragonArr::only($target, DragonArr::keys($source)) : $target; } + + public function ksort(array $source): array + { + ksort($source); + + foreach ($source as $key => &$value) { + if (is_array($value)) { + $value = $this->ksort($value); + } + } + + return $source; + } } diff --git a/src/Processors/Processor.php b/src/Processors/Processor.php index 5f08c256..846096cc 100644 --- a/src/Processors/Processor.php +++ b/src/Processors/Processor.php @@ -51,9 +51,9 @@ public function __construct( readonly protected Config $config, protected Manager $filesystem = new Manager(), protected ArrHelper $arr = new ArrHelper(), - protected Translation $translation = new Translation() - ) { - } + protected Translation $translation = new Translation( + ) + ) {} public function prepare(): self { @@ -71,9 +71,8 @@ function () use ($directory, $plugins) { /** @var Plugin $plugin */ foreach ($plugins as $plugin) { $this->collectKeys($directory, $plugin->files()); + $this->collectLocalizations($directory, $plugin->files()); } - - $this->collectLocalizations($directory); } ); } @@ -91,7 +90,7 @@ public function store(): void $path = $this->config->langPath($filename); $values - = $this->reset || !File::exists($path) + = $this->reset || ! File::exists($path) ? $values : $this->arr->merge( $this->filesystem->load($path), @@ -109,28 +108,34 @@ protected function collectKeys(string $directory, array $files): void foreach ($files as $source => $target) { $values = $this->filesystem->load($directory . '/source/' . $source); - $this->translation->setSource($directory, $target, $values); + $this->translation->setSource($target, $values); } } - protected function collectLocalizations(string $directory): void + protected function collectLocalizations(string $directory, array $files): void { - foreach ($this->locales as $locale) { - $locale = $this->fromAlias($locale?->value ?? $locale); + foreach ($files as $filename) { + $keys = array_keys($this->translation->getSource($filename)); - $locale_alias = $this->toAlias($locale); + foreach ($this->locales as $locale) { + $locale = $this->fromAlias($locale?->value ?? $locale); - foreach ($this->file_types as $type) { - $main_path = $this->localeFilename($locale_alias, "$directory/locales/$locale/$type.json"); - $inline_path = $this->localeFilename($locale_alias, "$directory/locales/$locale/$type.json", true); + $locale_alias = $this->toAlias($locale); - $values = $this->filesystem->load($main_path); + foreach ($this->file_types as $type) { + $main_path = $this->localeFilename($locale_alias, "$directory/locales/$locale/$type.json"); + $inline_path = $this->localeFilename($locale_alias, "$directory/locales/$locale/$type.json", true); - if ($main_path !== $inline_path && $this->config->hasInline()) { - $values = $this->arr->merge($values, $this->filesystem->load($inline_path)); - } + $values = $this->filesystem->load($main_path); - $this->translation->setTranslations($directory, $locale_alias, $values); + if ($main_path !== $inline_path && $this->config->hasInline()) { + $values = $this->arr->merge($values, $this->filesystem->load($inline_path)); + } + + $values = collect($values)->only($keys)->toArray(); + + $this->translation->setTranslations($filename, $locale_alias, $values); + } } } } diff --git a/src/Resources/Translation.php b/src/Resources/Translation.php index ef25986f..4933377f 100644 --- a/src/Resources/Translation.php +++ b/src/Resources/Translation.php @@ -29,12 +29,16 @@ class Translation implements Arrayable public function __construct( readonly protected Arr $arr = new Arr() - ) { + ) {} + + public function getSource(string $filename): array + { + return $this->source[$filename] ?? []; } - public function setSource(string $namespace, string $filename, array $values): self + public function setSource(string $filename, array $values): self { - $this->source[$namespace][$filename] = $this->merge($this->source[$namespace][$filename] ?? [], $values); + $this->source[$filename] = $this->merge($this->source[$filename] ?? [], $values); return $this; } @@ -53,27 +57,15 @@ public function toArray(): array { $result = []; - foreach (array_keys($this->source) as $namespace) { - if (!isset($this->source[$namespace])) { - continue; - } - - foreach ($this->source[$namespace] as $filename => $keys) { - if (!isset($this->translations[$namespace])) { - continue; - } - - foreach ($this->translations[$namespace] as $locale => $values) { - $name = $this->resolveFilename($filename, $locale); + foreach ($this->source as $filename => $keys) { + foreach ($this->translations[$filename] ?? [] as $locale => $values) { + $name = $this->resolveFilename($filename, $locale); - $result[$locale][$name] = $this->merge($keys, $values, true); - } + $result[$locale][$name] = $this->merge($keys, $values, true); } } - ksort($result); - - return $result; + return $this->arr->ksort($result); } protected function resolveFilename(string $path, string $locale): string