From 41bd2c15386b80ecc1f8da8419d95dcd18adc49e Mon Sep 17 00:00:00 2001 From: Victor Schedlyn GUTT <39224143+VicGUTT@users.noreply.github.com> Date: Tue, 30 Jan 2024 16:55:48 +0100 Subject: [PATCH] [10.x] Fix - The `Translator` may incorrectly report the locale of a missing translation key (#49900) * fix: "Translator" returns correct locale for missing keys * chore: variable rename --- src/Illuminate/Translation/Translator.php | 4 ++-- tests/Integration/Translation/TranslatorTest.php | 13 +++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Translation/Translator.php b/src/Illuminate/Translation/Translator.php index 634a102d54c7..7b9ab9a56d6c 100755 --- a/src/Illuminate/Translation/Translator.php +++ b/src/Illuminate/Translation/Translator.php @@ -160,9 +160,9 @@ public function get($key, array $replace = [], $locale = null, $fallback = true) // the translator was instantiated. Then, we can load the lines and return. $locales = $fallback ? $this->localeArray($locale) : [$locale]; - foreach ($locales as $locale) { + foreach ($locales as $languageLineLocale) { if (! is_null($line = $this->getLine( - $namespace, $group, $locale, $item, $replace + $namespace, $group, $languageLineLocale, $item, $replace ))) { return $line; } diff --git a/tests/Integration/Translation/TranslatorTest.php b/tests/Integration/Translation/TranslatorTest.php index 5e374e82d1cb..de82aabce403 100644 --- a/tests/Integration/Translation/TranslatorTest.php +++ b/tests/Integration/Translation/TranslatorTest.php @@ -70,4 +70,17 @@ public function testItCanHandleMissingKeysNoReturn() $this->app['translator']->handleMissingKeysUsing(null); } + + public function testItReturnsCorrectLocaleForMissingKeys() + { + $this->app['translator']->handleMissingKeysUsing(function ($key, $replacements, $locale) { + $_SERVER['__missing_translation_key_locale'] = $locale; + }); + + $this->app['translator']->get('some missing key', [], 'ht'); + + $this->assertSame('ht', $_SERVER['__missing_translation_key_locale']); + + $this->app['translator']->handleMissingKeysUsing(null); + } }