Skip to content

Commit

Permalink
Create an abstract Locale middleware.
Browse files Browse the repository at this point in the history
Signed-off-by: crynobone <crynobone@gmail.com>
  • Loading branch information
crynobone committed Jun 10, 2015
1 parent d2d5b53 commit 0c95963
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 30 deletions.
36 changes: 6 additions & 30 deletions src/Middleware/BrowserLocale.php
Original file line number Diff line number Diff line change
@@ -1,40 +1,16 @@
<?php namespace Orchestra\Translation\Middleware;

use Closure;
use Illuminate\Contracts\Routing\Middleware;
use Illuminate\Contracts\Foundation\Application;

class BrowserLocale implements Middleware
class BrowserLocale extends Locale
{
/**
* The application implementation.
*
* @var \Illuminate\Contracts\Foundation\Application
*/
protected $app;

/**
* Create a new middleware instance.
*
* @param \Illuminate\Contracts\Foundation\Application $app
*/
public function __construct(Application $app)
{
$this->app = $app;
}

/**
* Handle an incoming request.
* Get current locale.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param \Illuminate\Http\Request $request
*
* @return mixed
* @return string
*/
public function handle($request, Closure $next)
protected function getCurrentLocale($request)
{
$this->app->setLocale($request->getPreferredLanguage());

return $next($request);
return $request->getPreferredLanguage();
}
}
48 changes: 48 additions & 0 deletions src/Middleware/Locale.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php namespace Orchestra\Translation\Middleware;

use Closure;
use Illuminate\Contracts\Foundation\Application;

abstract class Locale
{
/**
* The application implementation.
*
* @var \Illuminate\Contracts\Foundation\Application
*/
protected $app;

/**
* Create a new middleware instance.
*
* @param \Illuminate\Contracts\Foundation\Application $app
*/
public function __construct(Application $app)
{
$this->app = $app;
}

/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
*
* @return mixed
*/
public function handle($request, Closure $next)
{
$this->app->setLocale($this->getCurrentLocale($request));

return $next($request);
}

/**
* Get current locale.
*
* @param \Illuminate\Http\Request $request
*
* @return string
*/
abstract protected function getCurrentLocale($request);
}

0 comments on commit 0c95963

Please sign in to comment.