-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create an abstract Locale middleware.
Signed-off-by: crynobone <crynobone@gmail.com>
- Loading branch information
Showing
2 changed files
with
54 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |