Skip to content

Commit

Permalink
Fix for fix :)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrey-helldar committed Feb 21, 2024
1 parent d2a9822 commit 665cd13
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 39 deletions.
13 changes: 13 additions & 0 deletions src/Helpers/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
43 changes: 24 additions & 19 deletions src/Processors/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);
}
);
}
Expand All @@ -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),
Expand All @@ -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);
}
}
}
}
Expand Down
32 changes: 12 additions & 20 deletions src/Resources/Translation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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
Expand Down

0 comments on commit 665cd13

Please sign in to comment.