Skip to content

Commit 2006ec6

Browse files
Merge pull request #350 from Laravel-Lang/16.x
Fixed a bug with translation keys being mixed among different files
2 parents cdae059 + d2a9822 commit 2006ec6

File tree

2 files changed

+48
-33
lines changed

2 files changed

+48
-33
lines changed

src/Processors/Processor.php

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ public function __construct(
5151
readonly protected Config $config,
5252
protected Manager $filesystem = new Manager(),
5353
protected ArrHelper $arr = new ArrHelper(),
54-
protected Translation $translation = new Translation(
55-
)
56-
) {}
54+
protected Translation $translation = new Translation()
55+
) {
56+
}
5757

5858
public function prepare(): self
5959
{
@@ -62,17 +62,20 @@ public function prepare(): self
6262

6363
public function collect(): self
6464
{
65+
$this->info('Collecting translations...');
66+
6567
foreach ($this->plugins() as $directory => $plugins) {
66-
$this->info($this->config->getPackageNameByPath($directory, Types::TypeClass));
68+
$this->task(
69+
$this->config->getPackageNameByPath($directory, Types::TypeClass),
70+
function () use ($directory, $plugins) {
71+
/** @var Plugin $plugin */
72+
foreach ($plugins as $plugin) {
73+
$this->collectKeys($directory, $plugin->files());
74+
}
6775

68-
$this->task('Collect source', function () use ($directory, $plugins) {
69-
/** @var Plugin $plugin */
70-
foreach ($plugins as $plugin) {
71-
$this->collectKeys($directory, $plugin->files());
76+
$this->collectLocalizations($directory);
7277
}
73-
});
74-
75-
$this->collectLocalizations($directory);
78+
);
7679
}
7780

7881
return $this;
@@ -88,7 +91,7 @@ public function store(): void
8891
$path = $this->config->langPath($filename);
8992

9093
$values
91-
= $this->reset || ! File::exists($path)
94+
= $this->reset || !File::exists($path)
9295
? $values
9396
: $this->arr->merge(
9497
$this->filesystem->load($path),
@@ -106,7 +109,7 @@ protected function collectKeys(string $directory, array $files): void
106109
foreach ($files as $source => $target) {
107110
$values = $this->filesystem->load($directory . '/source/' . $source);
108111

109-
$this->translation->setSource($target, $values);
112+
$this->translation->setSource($directory, $target, $values);
110113
}
111114
}
112115

@@ -117,20 +120,18 @@ protected function collectLocalizations(string $directory): void
117120

118121
$locale_alias = $this->toAlias($locale);
119122

120-
$this->task('Collecting ' . $locale, function () use ($locale, $locale_alias, $directory) {
121-
foreach ($this->file_types as $type) {
122-
$main_path = $this->localeFilename($locale_alias, "$directory/locales/$locale/$type.json");
123-
$inline_path = $this->localeFilename($locale_alias, "$directory/locales/$locale/$type.json", true);
123+
foreach ($this->file_types as $type) {
124+
$main_path = $this->localeFilename($locale_alias, "$directory/locales/$locale/$type.json");
125+
$inline_path = $this->localeFilename($locale_alias, "$directory/locales/$locale/$type.json", true);
124126

125-
$values = $this->filesystem->load($main_path);
127+
$values = $this->filesystem->load($main_path);
126128

127-
if ($main_path !== $inline_path && $this->config->hasInline()) {
128-
$values = $this->arr->merge($values, $this->filesystem->load($inline_path));
129-
}
130-
131-
$this->translation->setTranslations($locale_alias, $values);
129+
if ($main_path !== $inline_path && $this->config->hasInline()) {
130+
$values = $this->arr->merge($values, $this->filesystem->load($inline_path));
132131
}
133-
});
132+
133+
$this->translation->setTranslations($directory, $locale_alias, $values);
134+
}
134135
}
135136
}
136137

src/Resources/Translation.php

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,22 @@ class Translation implements Arrayable
2929

3030
public function __construct(
3131
readonly protected Arr $arr = new Arr()
32-
) {}
32+
) {
33+
}
3334

34-
public function setSource(string $filename, array $values): self
35+
public function setSource(string $namespace, string $filename, array $values): self
3536
{
36-
$this->source[$filename] = $this->merge($this->source[$filename] ?? [], $values);
37+
$this->source[$namespace][$filename] = $this->merge($this->source[$namespace][$filename] ?? [], $values);
3738

3839
return $this;
3940
}
4041

41-
public function setTranslations(string $locale, array $values): self
42+
public function setTranslations(string $namespace, string $locale, array $values): self
4243
{
43-
$this->translations[$locale] = $this->merge($this->translations[$locale] ?? [], $values);
44+
$this->translations[$namespace][$locale] = $this->merge(
45+
$this->translations[$namespace][$locale] ?? [],
46+
$values
47+
);
4448

4549
return $this;
4650
}
@@ -49,11 +53,21 @@ public function toArray(): array
4953
{
5054
$result = [];
5155

52-
foreach ($this->source as $filename => $keys) {
53-
foreach ($this->translations as $locale => $values) {
54-
$name = $this->resolveFilename($filename, $locale);
56+
foreach (array_keys($this->source) as $namespace) {
57+
if (!isset($this->source[$namespace])) {
58+
continue;
59+
}
60+
61+
foreach ($this->source[$namespace] as $filename => $keys) {
62+
if (!isset($this->translations[$namespace])) {
63+
continue;
64+
}
65+
66+
foreach ($this->translations[$namespace] as $locale => $values) {
67+
$name = $this->resolveFilename($filename, $locale);
5568

56-
$result[$locale][$name] = $this->merge($keys, $values, true);
69+
$result[$locale][$name] = $this->merge($keys, $values, true);
70+
}
5771
}
5872
}
5973

0 commit comments

Comments
 (0)