Skip to content

Commit

Permalink
Remove dependency on Nette\Http\Request
Browse files Browse the repository at this point in the history
  • Loading branch information
Rixafy committed Oct 13, 2024
1 parent f76d3e9 commit 067a1cc
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 150 deletions.
60 changes: 44 additions & 16 deletions src/LocalesResolvers/Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,81 @@

namespace Contributte\Translation\LocalesResolvers;

use Contributte\Translation\Exceptions\InvalidArgument;
use Contributte\Translation\Translator;
use Nette\Http\IRequest;
use Nette\Http\Request;
use Nette\Utils\Strings;

class Header implements ResolverInterface
{

private Request $httpRequest;
private IRequest $httpRequest;

/**
* @throws \Contributte\Translation\Exceptions\InvalidArgument
*/
public function __construct(
IRequest $httpRequest
)
{
if (!is_a($httpRequest, Request::class, true)) {
throw new InvalidArgument('Header locale resolver need "Nette\\Http\\Request" or his child for using "detectLanguage" method.');
}

$this->httpRequest = $httpRequest;
}

public function resolve(
Translator $translator
): ?string
{
/** @var array<string> $langs */
$langs = [];
/** @var array<string> $locales */
$locales = [];

foreach ($translator->getAvailableLocales() as $v1) {
$langs[] = $v1;
$locales[] = $v1;

if (Strings::length($v1) < 3) {
continue;
}

$langs[] = Strings::substring($v1, 0, 2);// en_US => en
$locales[] = Strings::substring($v1, 0, 2);// en_US => en
}

if (count($langs) === 0) {
if (count($locales) === 0) {
return null;
}

return $this->httpRequest->detectLanguage($langs);
return $this->detectLocale($locales);
}

/**
* This function is a copy of \Nette\Http\Request::detectLanguage()
* Returns the most preferred locale by browser. Uses the `Accept-Language` header. If no match is reached, it returns `null`.
*
* @param array<string> $supportedLocales
*/
private function detectLocale(array $supportedLocales): ?string
{
$header = $this->httpRequest->getHeader('Accept-Language');
if ($header === null) {
return null;
}

rsort($supportedLocales); // first more specific
preg_match_all(
'#(' . implode('|', $supportedLocales) . ')(?:-[^\s,;=]+)?\s*(?:;\s*q=([0-9.]+))?#',
strtr(strtolower($header), '_', '-'), // CS_CZ -> cs-CZ
$matches
);

if (count($matches) === 0) {
return null;
}

$max = 0;
$locale = null;
foreach ($matches[1] as $key => $value) {
$q = $matches[2][$key] === '' ? 1.0 : (float) $matches[2][$key];
if ($q > $max) {
$max = $q;
$locale = $value;
}
}

return $locale;
}

}
134 changes: 0 additions & 134 deletions tests/Tests/LocalesResolvers/HeaderTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@

namespace Tests\LocalesResolvers;

use Contributte\Translation\Exceptions\InvalidArgument;
use Contributte\Translation\LocalesResolvers\Header;
use Contributte\Translation\Translator;
use Mockery;
use Nette\Http\IRequest;
use Nette\Http\Request;
use Nette\Http\UrlImmutable;
use Nette\Http\UrlScript;
use Tester\Assert;
use Tests\TestAbstract;
Expand All @@ -31,137 +28,6 @@ final class HeaderTest extends TestAbstract
Assert::same('en-us', $this->resolve('da, en_us', ['en', 'en-us']));
}

public function test02(): void
{
Assert::exception(static function (): void {
new Header(new class implements IRequest {

public function getReferer(): ?UrlImmutable
{
return null;
}

public function isSameSite(): bool
{
return true;
}

public function getUrl(): UrlScript
{
return new UrlScript();
}

/**
* @return mixed
*/
public function getQuery(
?string $key = null
)
{
return null;
}

/**
* @return mixed
*/
public function getPost(
?string $key = null
)
{
return null;
}

/**
* @return mixed
*/
public function getFile(
string $key
)
{
return null;
}

/**
* @return array<\Nette\Http\FileUpload>
*/
public function getFiles(): array
{
return [];
}

/**
* @return mixed
*/
public function getCookie(
string $key
)
{
return null;
}

/**
* @return array<string>
*/
public function getCookies(): array
{
return [];
}

public function getMethod(): string
{
return '';
}

public function isMethod(
string $method
): bool
{
return true;
}
public function getHeader(
string $header
): ?string
{
return null;
}

/**
* @return array<string>
*/
public function getHeaders(): array
{
return [];
}

public function isSecured(): bool
{
return true;
}

public function isAjax(): bool
{
return true;
}

public function getRemoteAddress(): ?string
{
return null;
}

public function getRemoteHost(): ?string
{
return null;
}

public function getRawBody(): ?string
{
return null;
}

});
}, InvalidArgument::class, 'Header locale resolver need "' . Request::class . '" or his child for using "detectLanguage" method.');
}

/**
* @param array<string> $availableLocales
*/
Expand Down

0 comments on commit 067a1cc

Please sign in to comment.