Skip to content

Commit

Permalink
Merge pull request #3 from locomotivemtl/locale-from-host
Browse files Browse the repository at this point in the history
Setting locale from domain/host
  • Loading branch information
dominiclord authored Oct 17, 2017
2 parents be854fb + 534093d commit 06dd953
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/Charcoal/Translator/Middleware/LanguageMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ class LanguageMiddleware
*/
private $paramKey;

/**
* @var boolean
*/
private $useHost;

/**
* @var array
*/
private $hostMap;

/**
* @var boolean
*/
Expand Down Expand Up @@ -100,6 +110,9 @@ public function __construct(array $data)

$this->useBrowser = !!$data['use_browser'];

$this->useHost = !!$data['use_host'];
$this->hostMap = (array)$data['host_map'];

$this->setLocale = !!$data['set_locale'];
}

Expand All @@ -126,6 +139,9 @@ public function defaults()

'use_browser' => true,

'use_host' => false,
'host_map' => [],

'set_locale' => true
];
}
Expand Down Expand Up @@ -159,6 +175,13 @@ public function __invoke(RequestInterface $request, ResponseInterface $response,
*/
private function getLanguage(RequestInterface $request)
{
if ($this->useHost === true) {
$lang = $this->getLanguageFromHost($request);
if ($lang) {
return $lang;
}
}

if ($this->usePath === true) {
$lang = $this->getLanguageFromPath($request);
if ($lang) {
Expand Down Expand Up @@ -190,6 +213,20 @@ private function getLanguage(RequestInterface $request)
return $this->defaultLanguage;
}

/**
* @param RequestInterface $request The PSR-7 HTTP request.
* @return null|string
*/
private function getLanguageFromHost(RequestInterface $request)
{
$uriHost = $request->getUri()->getHost();
foreach ($this->hostMap as $lang => $host) {
if (stripos($uriHost, $host) !== false) {
return $lang;
}
}
}

/**
* @param RequestInterface $request The PSR-7 HTTP request.
* @return null|string
Expand Down

0 comments on commit 06dd953

Please sign in to comment.