Skip to content

[12.x] Prevent dupe locale checks in Lang::get() when locale matches fallback#58626

Merged
taylorotwell merged 3 commits intolaravel:12.xfrom
jackbayliss:12.x-translator-localeArray-once
Feb 4, 2026
Merged

[12.x] Prevent dupe locale checks in Lang::get() when locale matches fallback#58626
taylorotwell merged 3 commits intolaravel:12.xfrom
jackbayliss:12.x-translator-localeArray-once

Conversation

@jackbayliss
Copy link
Contributor

@jackbayliss jackbayliss commented Feb 4, 2026

Closes #58597

Essentially if you used the same locale in Lang::get(), as the fallback, it would cause the same locale to be checked twice.

// Set the fallback... could be in our service provider
Lang::setFallback('en')


// Elsewhere we call...
Lang::get(key: 'header', locale: 'en') // same as our fallback  - IRL this could be $user->locale


// Internally.. localeArray returns
array:2 [
  0 => "en"
  1 => "en"
]

Internally here, because the fallback matches the set locale, it causes the localeArray method to return `['en,'en'] - meaning the same locale gets checked 2* for the same key.

I've added array_values to re-index the array so there should be no B/C.

I've added a test - but you can remove it if its' a bit OTT, was just to prove it works.

TLDR is tiny efficiency boost / bug fix?

We must SHIP

Thanks!

@github-actions
Copy link

github-actions bot commented Feb 4, 2026

Thanks for submitting a PR!

Note that draft PRs are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface.

Pull requests that are abandoned in draft may be closed due to inactivity.

@jackbayliss jackbayliss marked this pull request as ready for review February 4, 2026 22:54
@taylorotwell taylorotwell merged commit 65e05ad into laravel:12.x Feb 4, 2026
70 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.

Illuminate\Translation\Translator::localeArray may return duplicate locales

2 participants

Comments