Skip to content

Commit a19ddcc

Browse files
fix: custom translator not receiving updated locale (#919)
1 parent fea226d commit a19ddcc

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/Mcamara/LaravelLocalization/LaravelLocalization.php

+1
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ public function setLocale($locale = null)
194194
}
195195

196196
$this->app->setLocale($this->currentLocale);
197+
$this->translator->setLocale($this->currentLocale);
197198

198199
// Regional locale such as de_DE, so formatLocalized works in Carbon
199200
$regional = $this->getCurrentLocaleRegional();

tests/CustomTranslatorTest.php

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Mcamara\LaravelLocalization\Tests;
4+
5+
use Illuminate\Translation\ArrayLoader;
6+
use Illuminate\Translation\Translator;
7+
use Mcamara\LaravelLocalization\LaravelLocalization;
8+
9+
class CustomTranslatorTest extends TestCase
10+
{
11+
// The LaravelLocalization class supports the use of a custom translator instance.
12+
// When the setLocale() method is invoked, it delegates the operation to the application instance,
13+
// which, in turn, updates the locale on the translator instance bound in the service container.
14+
// If a custom translator is provided, LaravelLocalization ensures that the locale is also set correctly
15+
// on the custom translator to maintain consistent behavior across the application.
16+
public function testCanSetLocaleForCustomTranslator(): void
17+
{
18+
$loader = new ArrayLoader();
19+
$translator = new Translator($loader , 'en');
20+
$localization = new LaravelLocalization(
21+
$this->app,
22+
$this->app['config'],
23+
$translator,
24+
$this->app['router'],
25+
$this->app['request'],
26+
$this->app['url']
27+
);
28+
29+
$localization->setLocale('es');
30+
31+
$this->assertEquals('es', $translator->getLocale());
32+
}
33+
}

0 commit comments

Comments
 (0)