Skip to content

[12.x] Fix translation FileLoader overrides with a missing key #55342

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

Merged
merged 2 commits into from
Apr 11, 2025

Conversation

fabio-ivona
Copy link
Contributor

This PR will fix the translations FileLoader behaviour when multiple overrides are set up and the last one is missing a translation key:

as in Fileloader → loadNamespaceOverrides() the $lines variable is passed to the function through use, its value is used every time in array_replace_recursive(), instead of using the updated $output variable coming from previous iterations of the reduce function

this will cause the last iteration to remove all overrides made in previous ones

I added a failing test for this issue and implemented the fix:

protected function loadNamespaceOverrides(array $lines, $locale, $group, $namespace)
    {
        return (new Collection($this->paths))
-            ->reduce(function ($output, $path) use ($lines, $locale, $group, $namespace) {
+           ->reduce(function ($output, $path) use ($locale, $group, $namespace) {
                $file = "{$path}/vendor/{$namespace}/{$locale}/{$group}.php";

                if ($this->files->exists($file)) {
-                    $lines = array_replace_recursive($lines, $this->files->getRequire($file));
+                    $output = array_replace_recursive($output, $this->files->getRequire($file));
                }

-                return $lines;
+                return $output;
-            }, []);
+            }, $lines);
    }

@fabio-ivona fabio-ivona changed the title [12.x] Fix translation FileLoader overrides when a missing key [12.x] Fix translation FileLoader overrides with a missing key Apr 9, 2025
@taylorotwell taylorotwell merged commit b8f4f0d into laravel:12.x Apr 11, 2025
60 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants