-
Notifications
You must be signed in to change notification settings - Fork 11.8k
Description
Laravel Version
12.44.0
PHP Version
8.4.11
Database Driver & Version
No response
Description
When $this->locale and $this->fallback are same values (e.g. en), the following method returns an array with duplicates:
/**
* Get the array of locales to be checked.
*
* @param string|null $locale
* @return array
*/
protected function localeArray($locale)
{
$locales = array_filter([$locale ?: $this->locale, $this->fallback]);
return call_user_func($this->determineLocalesUsing ?: fn () => $locales, $locales);
}
These duplicates are used later in the Translator::get() method where a loop tries to find a translation:
$locales = $fallback ? $this->localeArray($locale) : [$locale];
foreach ($locales as $languageLineLocale) {
if (! is_null($line = $this->getLine(
$namespace, $group, $languageLineLocale, $item, $replace
))) {
return $line;
}
}
When a translation can't be found from the first attempt, it creates a condition when $this->getLine(...) is called twice for no good reason.
In my opinion, either Translator::localeArray method should return only unique values, or Translator::get method should ignore duplicate locales.
Steps To Reproduce
The following call can be used to set a fallback locale:
\Illuminate\Support\Facades\Lang::setFallback('en')
The bug can be reproduced by calling a Translator::get() method where $locale parameter matches the fallback and where the $key parameter does not exist in the translation file:
\Illuminate\Support\Facades\Lang::get(key: 'abcd', locale: 'en')