Locale: Set initial locale using request language

main
Igor Scheller 4 years ago committed by msquare
parent 30e1d41c0a
commit c2b2487721

@ -4,6 +4,7 @@ namespace Engelsystem\Helpers\Translation;
use Engelsystem\Config\Config; use Engelsystem\Config\Config;
use Engelsystem\Container\ServiceProvider; use Engelsystem\Container\ServiceProvider;
use Engelsystem\Http\Request;
use Gettext\Loader\MoLoader; use Gettext\Loader\MoLoader;
use Gettext\Loader\PoLoader; use Gettext\Loader\PoLoader;
use Gettext\Translations; use Gettext\Translations;
@ -21,10 +22,13 @@ class TranslationServiceProvider extends ServiceProvider
$config = $this->app->get('config'); $config = $this->app->get('config');
/** @var Session $session */ /** @var Session $session */
$session = $this->app->get('session'); $session = $this->app->get('session');
/** @var Request $request */
$request = $this->app->get('request');
$locales = $config->get('locales'); $locales = $config->get('locales');
$locale = $config->get('default_locale'); $defaultLocale = $config->get('default_locale');
$fallbackLocale = $config->get('fallback_locale', 'en_US'); $fallbackLocale = $config->get('fallback_locale', 'en_US');
$locale = $request->getPreferredLanguage(array_merge([$defaultLocale], array_keys($locales)));
$sessionLocale = $session->get('locale', $locale); $sessionLocale = $session->get('locale', $locale);
if (isset($locales[$sessionLocale])) { if (isset($locales[$sessionLocale])) {

@ -2,6 +2,7 @@
namespace Engelsystem\Middleware; namespace Engelsystem\Middleware;
use Engelsystem\Helpers\Authenticator;
use Engelsystem\Helpers\Translation\Translator; use Engelsystem\Helpers\Translation\Translator;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
@ -11,6 +12,9 @@ use Symfony\Component\HttpFoundation\Session\Session;
class SetLocale implements MiddlewareInterface class SetLocale implements MiddlewareInterface
{ {
/** @var Authenticator */
protected $auth;
/** @var Translator */ /** @var Translator */
protected $translator; protected $translator;
@ -20,9 +24,11 @@ class SetLocale implements MiddlewareInterface
/** /**
* @param Translator $translator * @param Translator $translator
* @param Session $session * @param Session $session
* @param Authenticator $auth
*/ */
public function __construct(Translator $translator, Session $session) public function __construct(Translator $translator, Session $session, Authenticator $auth)
{ {
$this->auth = $auth;
$this->translator = $translator; $this->translator = $translator;
$this->session = $session; $this->session = $session;
} }
@ -42,6 +48,12 @@ class SetLocale implements MiddlewareInterface
$this->translator->setLocale($locale); $this->translator->setLocale($locale);
$this->session->set('locale', $locale); $this->session->set('locale', $locale);
$user = $this->auth->user();
if ($user) {
$user->settings->language = $locale;
$user->settings->save();
}
} }
return $handler->handle($request); return $handler->handle($request);

@ -5,6 +5,7 @@ namespace Engelsystem\Test\Unit\Helpers\Translation;
use Engelsystem\Config\Config; use Engelsystem\Config\Config;
use Engelsystem\Helpers\Translation\TranslationServiceProvider; use Engelsystem\Helpers\Translation\TranslationServiceProvider;
use Engelsystem\Helpers\Translation\Translator; use Engelsystem\Helpers\Translation\Translator;
use Engelsystem\Http\Request;
use Engelsystem\Test\Unit\ServiceProviderTest; use Engelsystem\Test\Unit\ServiceProviderTest;
use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\MockObject\Rule\InvokedCount; use PHPUnit\Framework\MockObject\Rule\InvokedCount;
@ -17,10 +18,9 @@ class TranslationServiceProviderTest extends ServiceProviderTest
*/ */
public function testRegister(): void public function testRegister(): void
{ {
$defaultLocale = 'fo_OO'; $locale = 'te_ST';
$locale = 'te_ST.WTF-9'; $locales = ['fo_OO' => 'Foo', 'fo_OO.BAR' => 'Foo (Bar)', 'te_ST' => 'WTF\'s Testing?'];
$locales = ['fo_OO' => 'Foo', 'fo_OO.BAR' => 'Foo (Bar)', 'te_ST.WTF-9' => 'WTF\'s Testing?']; $config = new Config(['locales' => $locales, 'default_locale' => 'fo_OO']);
$config = new Config(['locales' => $locales, 'default_locale' => $defaultLocale]);
$app = $this->getApp(['make', 'singleton', 'alias', 'get']); $app = $this->getApp(['make', 'singleton', 'alias', 'get']);
/** @var Session|MockObject $session */ /** @var Session|MockObject $session */
@ -34,14 +34,16 @@ class TranslationServiceProviderTest extends ServiceProviderTest
->onlyMethods(['setLocale']) ->onlyMethods(['setLocale'])
->getMock(); ->getMock();
$app->expects($this->exactly(2)) $request = (new Request())->withAddedHeader('Accept-Language', 'te_ST');
$app->expects($this->exactly(3))
->method('get') ->method('get')
->withConsecutive(['config'], ['session']) ->withConsecutive(['config'], ['session'], ['request'])
->willReturnOnConsecutiveCalls($config, $session); ->willReturnOnConsecutiveCalls($config, $session, $request);
$session->expects($this->once()) $session->expects($this->once())
->method('get') ->method('get')
->with('locale', $defaultLocale) ->with('locale', 'te_ST')
->willReturn($locale); ->willReturn($locale);
$session->expects($this->once()) $session->expects($this->once())
->method('set') ->method('set')

@ -2,10 +2,14 @@
namespace Engelsystem\Test\Unit\Middleware; namespace Engelsystem\Test\Unit\Middleware;
use Engelsystem\Helpers\Authenticator;
use Engelsystem\Helpers\Translation\Translator; use Engelsystem\Helpers\Translation\Translator;
use Engelsystem\Middleware\SetLocale; use Engelsystem\Middleware\SetLocale;
use Engelsystem\Models\User\Settings;
use Engelsystem\Models\User\User;
use Engelsystem\Test\Unit\HasDatabase;
use Engelsystem\Test\Unit\TestCase;
use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface; use Psr\Http\Server\RequestHandlerInterface;
@ -13,12 +17,18 @@ use Symfony\Component\HttpFoundation\Session\Session;
class SetLocaleTest extends TestCase class SetLocaleTest extends TestCase
{ {
use HasDatabase;
/** /**
* @covers \Engelsystem\Middleware\SetLocale::__construct * @covers \Engelsystem\Middleware\SetLocale::__construct
* @covers \Engelsystem\Middleware\SetLocale::process * @covers \Engelsystem\Middleware\SetLocale::process
*/ */
public function testRegister() public function testRegister()
{ {
$this->initDatabase();
/** @var Authenticator|MockObject $auth */
$auth = $this->createMock(Authenticator::class);
/** @var Translator|MockObject $translator */ /** @var Translator|MockObject $translator */
$translator = $this->createMock(Translator::class); $translator = $this->createMock(Translator::class);
/** @var Session|MockObject $session */ /** @var Session|MockObject $session */
@ -30,20 +40,30 @@ class SetLocaleTest extends TestCase
/** @var ResponseInterface|MockObject $response */ /** @var ResponseInterface|MockObject $response */
$response = $this->getMockForAbstractClass(ResponseInterface::class); $response = $this->getMockForAbstractClass(ResponseInterface::class);
$locale = 'te_ST.UTF8'; $user = User::create([
'name' => 'user',
'password' => '',
'email' => 'foo@bar.baz',
'api_key' => '',
]);
$settings = new Settings(['language' => 'uf_UF', 'theme' => '']);
$settings->user()->associate($user);
$settings->save();
$locale = 'te_ST';
$request->expects($this->exactly(3)) $request->expects($this->exactly(3))
->method('getQueryParams') ->method('getQueryParams')
->willReturnOnConsecutiveCalls( ->willReturnOnConsecutiveCalls(
[], [],
['set-locale' => 'en_US.UTF8'], ['set-locale' => 'en_US'],
['set-locale' => $locale] ['set-locale' => $locale]
); );
$translator->expects($this->exactly(2)) $translator->expects($this->exactly(2))
->method('hasLocale') ->method('hasLocale')
->withConsecutive( ->withConsecutive(
['en_US.UTF8'], ['en_US'],
[$locale] [$locale]
) )
->willReturnOnConsecutiveCalls( ->willReturnOnConsecutiveCalls(
@ -63,9 +83,15 @@ class SetLocaleTest extends TestCase
->with($request) ->with($request)
->willReturn($response); ->willReturn($response);
$middleware = new SetLocale($translator, $session); $this->setExpects($auth, 'user', null, $user);
$middleware = new SetLocale($translator, $session, $auth);
$middleware->process($request, $handler); $middleware->process($request, $handler);
$middleware->process($request, $handler); $middleware->process($request, $handler);
$this->assertEquals('uf_UF', $user->settings->language);
$middleware->process($request, $handler); $middleware->process($request, $handler);
$this->assertEquals('te_ST', $user->settings->language);
} }
} }

Loading…
Cancel
Save