Skip to content

Commit

Permalink
Merge pull request #351 from Laravel-Lang/16.x
Browse files Browse the repository at this point in the history
Fix for fix :)
  • Loading branch information
andrey-helldar committed Feb 21, 2024
2 parents e62d571 + 2b069d1 commit 3515797
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 34 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;
}
}
35 changes: 20 additions & 15 deletions src/Processors/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 Down Expand Up @@ -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);

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($directory, $locale_alias, $values);
$this->translation->setTranslations($filename, $locale_alias, $values);
}
}
}
}
Expand Down
31 changes: 12 additions & 19 deletions src/Resources/Translation.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,14 @@ public function __construct(
readonly protected Arr $arr = new Arr()
) {}

public function setSource(string $namespace, string $filename, array $values): self
public function getSource(string $filename): array
{
$this->source[$namespace][$filename] = $this->merge($this->source[$namespace][$filename] ?? [], $values);
return $this->source[$filename] ?? [];
}

public function setSource(string $filename, array $values): self
{
$this->source[$filename] = $this->merge($this->source[$filename] ?? [], $values);

return $this;
}
Expand All @@ -52,27 +57,15 @@ public function toArray(): array
{
$result = [];

foreach (array_keys($this->source) as $namespace) {
if (! isset($this->source[$namespace])) {
continue;
}
foreach ($this->source as $filename => $keys) {
foreach ($this->translations[$filename] ?? [] as $locale => $values) {
$name = $this->resolveFilename($filename, $locale);

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);

$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 3515797

Please sign in to comment.