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

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

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

@ -2,10 +2,14 @@
namespace Engelsystem\Test\Unit\Middleware;
use Engelsystem\Helpers\Authenticator;
use Engelsystem\Helpers\Translation\Translator;
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\TestCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
@ -13,12 +17,18 @@ use Symfony\Component\HttpFoundation\Session\Session;
class SetLocaleTest extends TestCase
{
use HasDatabase;
/**
* @covers \Engelsystem\Middleware\SetLocale::__construct
* @covers \Engelsystem\Middleware\SetLocale::process
*/
public function testRegister()
{
$this->initDatabase();
/** @var Authenticator|MockObject $auth */
$auth = $this->createMock(Authenticator::class);
/** @var Translator|MockObject $translator */
$translator = $this->createMock(Translator::class);
/** @var Session|MockObject $session */
@ -30,20 +40,30 @@ class SetLocaleTest extends TestCase
/** @var ResponseInterface|MockObject $response */
$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))
->method('getQueryParams')
->willReturnOnConsecutiveCalls(
[],
['set-locale' => 'en_US.UTF8'],
['set-locale' => 'en_US'],
['set-locale' => $locale]
);
$translator->expects($this->exactly(2))
->method('hasLocale')
->withConsecutive(
['en_US.UTF8'],
['en_US'],
[$locale]
)
->willReturnOnConsecutiveCalls(
@ -63,9 +83,15 @@ class SetLocaleTest extends TestCase
->with($request)
->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);
$this->assertEquals('uf_UF', $user->settings->language);
$middleware->process($request, $handler);
$this->assertEquals('te_ST', $user->settings->language);
}
}

Loading…
Cancel
Save