Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed a bug with translation keys being mixed among different files #350

Merged
merged 1 commit into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 25 additions & 24 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 @@ -62,17 +62,20 @@ public function prepare(): self

public function collect(): self
{
$this->info('Collecting translations...');

foreach ($this->plugins() as $directory => $plugins) {
$this->info($this->config->getPackageNameByPath($directory, Types::TypeClass));
$this->task(
$this->config->getPackageNameByPath($directory, Types::TypeClass),
function () use ($directory, $plugins) {
/** @var Plugin $plugin */
foreach ($plugins as $plugin) {
$this->collectKeys($directory, $plugin->files());
}

$this->task('Collect source', function () use ($directory, $plugins) {
/** @var Plugin $plugin */
foreach ($plugins as $plugin) {
$this->collectKeys($directory, $plugin->files());
$this->collectLocalizations($directory);
}
});

$this->collectLocalizations($directory);
);
}

return $this;
Expand All @@ -88,7 +91,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 @@ -106,7 +109,7 @@ protected function collectKeys(string $directory, array $files): void
foreach ($files as $source => $target) {
$values = $this->filesystem->load($directory . '/source/' . $source);

$this->translation->setSource($target, $values);
$this->translation->setSource($directory, $target, $values);
}
}

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

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

$this->task('Collecting ' . $locale, function () use ($locale, $locale_alias, $directory) {
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);
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);

$values = $this->filesystem->load($main_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));
}

$this->translation->setTranslations($locale_alias, $values);
if ($main_path !== $inline_path && $this->config->hasInline()) {
$values = $this->arr->merge($values, $this->filesystem->load($inline_path));
}
});

$this->translation->setTranslations($directory, $locale_alias, $values);
}
}
}

Expand Down
32 changes: 23 additions & 9 deletions src/Resources/Translation.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,22 @@ class Translation implements Arrayable

public function __construct(
readonly protected Arr $arr = new Arr()
) {}
) {
}

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

return $this;
}

public function setTranslations(string $locale, array $values): self
public function setTranslations(string $namespace, string $locale, array $values): self
{
$this->translations[$locale] = $this->merge($this->translations[$locale] ?? [], $values);
$this->translations[$namespace][$locale] = $this->merge(
$this->translations[$namespace][$locale] ?? [],
$values
);

return $this;
}
Expand All @@ -49,11 +53,21 @@ public function toArray(): array
{
$result = [];

foreach ($this->source as $filename => $keys) {
foreach ($this->translations as $locale => $values) {
$name = $this->resolveFilename($filename, $locale);
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);

$result[$locale][$name] = $this->merge($keys, $values, true);
$result[$locale][$name] = $this->merge($keys, $values, true);
}
}
}

Expand Down