Skip to content

Commit 1b16d4b

Browse files
authored
[6.x] Revert "Fix translator locale" (#14358)
1 parent 77f65ff commit 1b16d4b

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

src/Http/Middleware/Localize.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,21 @@ public function handle($request, Closure $next)
1717
{
1818
$site = Site::current();
1919

20-
// Dates, Carbon, etc expect the full locale. (eg. "fr_FR" or whatever is
20+
// PHP date-formatting functions expect the full locale. (eg. "fr_FR" or whatever is
2121
// installed on your actual server. You can check by running `locale -a`).
2222
// We'll save the original locale so we can reset it later. Of course,
2323
// you can get the locale by calling the setlocale method. Logical.
2424
$originalLocale = setlocale(LC_TIME, 0);
2525
setlocale(LC_TIME, $site->locale());
2626

27-
// Use the site's lang for translations.
28-
$originalTranslatorLocale = app('translator')->getLocale();
29-
app('translator')->setLocale($site->lang());
27+
// The sites lang is used for your translations.
28+
// e.g. If you set your lang to "fr" it'll look for "fr" translations.
29+
// If not explicitly set, a site's lang will fall back to the "short locale"
30+
// e.g. If your site's locale is "fr_FR", the lang would be "fr".
31+
// Note that Carbon does also use this for some things.
32+
// Again, we'll save the original locale so we can reset it later.
33+
$originalAppLocale = app()->getLocale();
34+
app()->setLocale($site->lang());
3035

3136
// Get original Carbon format so it can be restored later.
3237
$originalToStringFormat = $this->getToStringFormat();
@@ -43,7 +48,7 @@ public function handle($request, Closure $next)
4348
// Reset everything back to their originals. This allows everything
4449
// not within the scope of the request to be the "defaults".
4550
setlocale(LC_TIME, $originalLocale);
46-
app('translator')->setLocale($originalTranslatorLocale);
51+
app()->setLocale($originalAppLocale);
4752
Date::setToStringFormat($originalToStringFormat);
4853

4954
return $response;

tests/FrontendTest.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -739,8 +739,6 @@ public function it_sets_the_carbon_to_string_format()
739739
#[Test]
740740
public function it_sets_the_locale()
741741
{
742-
// Frontend localization sets PHP LC_TIME and the translator locale for the site;
743-
// Laravel's configured app locale (app()->getLocale()) is left unchanged.
744742
// You can only set the locale to one that is actually installed on the server.
745743
// The names are a little different across jobs in the GitHub actions matrix.
746744
// We'll test against whichever was successfully applied. Finally, we will
@@ -768,16 +766,16 @@ public function index()
768766

769767
(new class extends Tags
770768
{
771-
public static $handle = 'translator_locale';
769+
public static $handle = 'laravel_locale';
772770

773771
public function index()
774772
{
775-
return app('translator')->getLocale();
773+
return app()->getLocale();
776774
}
777775
})->register();
778776

779777
$this->viewShouldReturnRaw('layout', '{{ template_content }}');
780-
$this->viewShouldReturnRaw('some_template', 'PHP Locale: {{ php_locale }} Translator Locale: {{ translator_locale }}');
778+
$this->viewShouldReturnRaw('some_template', 'PHP Locale: {{ php_locale }} App Locale: {{ laravel_locale }}');
781779

782780
$this->makeCollection()->sites(['english', 'french'])->save();
783781
tap($this->makePage('about', ['with' => ['template' => 'some_template']])->locale('english'))->save();
@@ -788,7 +786,7 @@ public function index()
788786

789787
$this->get('/fr/le-about')->assertSeeInOrder([
790788
'PHP Locale: '.$frLocale,
791-
'Translator Locale: fr',
789+
'App Locale: fr',
792790
]);
793791

794792
$this->assertEquals('en', app()->getLocale());

0 commit comments

Comments
 (0)