Skip to content

Commit

Permalink
fix: custom translator not receiving updated locale (#919)
Browse files Browse the repository at this point in the history
  • Loading branch information
jordyvanderhaegen authored Nov 27, 2024
1 parent fea226d commit a19ddcc
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Mcamara/LaravelLocalization/LaravelLocalization.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ public function setLocale($locale = null)
}

$this->app->setLocale($this->currentLocale);
$this->translator->setLocale($this->currentLocale);

// Regional locale such as de_DE, so formatLocalized works in Carbon
$regional = $this->getCurrentLocaleRegional();
Expand Down
33 changes: 33 additions & 0 deletions tests/CustomTranslatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Mcamara\LaravelLocalization\Tests;

use Illuminate\Translation\ArrayLoader;
use Illuminate\Translation\Translator;
use Mcamara\LaravelLocalization\LaravelLocalization;

class CustomTranslatorTest extends TestCase
{
// The LaravelLocalization class supports the use of a custom translator instance.
// When the setLocale() method is invoked, it delegates the operation to the application instance,
// which, in turn, updates the locale on the translator instance bound in the service container.
// If a custom translator is provided, LaravelLocalization ensures that the locale is also set correctly
// on the custom translator to maintain consistent behavior across the application.
public function testCanSetLocaleForCustomTranslator(): void
{
$loader = new ArrayLoader();
$translator = new Translator($loader , 'en');
$localization = new LaravelLocalization(
$this->app,
$this->app['config'],
$translator,
$this->app['router'],
$this->app['request'],
$this->app['url']
);

$localization->setLocale('es');

$this->assertEquals('es', $translator->getLocale());
}
}

0 comments on commit a19ddcc

Please sign in to comment.