Skip to content

Commit

Permalink
[10.x] Fix - The Translator may incorrectly report the locale of a …
Browse files Browse the repository at this point in the history
…missing translation key (#49900)

* fix: "Translator" returns correct locale for missing keys

* chore: variable rename
  • Loading branch information
VicGUTT authored Jan 30, 2024
1 parent 85f06e8 commit 41bd2c1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Illuminate/Translation/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
13 changes: 13 additions & 0 deletions tests/Integration/Translation/TranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 41bd2c1

Please sign in to comment.