Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: custom translator not receiving updated locale #919

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}
}
Loading