Skip to content

Commit

Permalink
add cookie feature #11 and disable encryption for cookie_name
Browse files Browse the repository at this point in the history
  • Loading branch information
vluzrmos committed Dec 30, 2015
1 parent fbf9a57 commit cb71dbc
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 7 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"require": {
"illuminate/config": "~5.0",
"illuminate/translation": "~5.0",
"illuminate/support": "~5.0"
"illuminate/support": "~5.0",
"illuminate/cookie": "~5.0"
},
"require-dev": {
"fabpot/php-cs-fixer": "~1.11",
Expand Down
5 changes: 5 additions & 0 deletions config/lang-detector.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
*/
'cookie' => (bool) env('LANG_DETECTOR_COOKIE', true),

/*
* Indicates if should encrypt cookie
*/
'cookie_encrypt' => (bool) env('LANG_DETECTOR_COOKIE_ENCRYPT', false),

/*
* Cookie name
*/
Expand Down
7 changes: 7 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,19 @@ Put that on your `.env` file:
```bash
#Indicates whenever should autodetect the language (it could be removed)
LANG_DETECTOR_AUTODETECT=true

#The driver to use, default is browser
LANG_DETECTOR_DRIVER="browser"

#The segment to use in uri or subdomain driver, default 0 (it could be removed)
LANG_DETECTOR_SEGMENT=0

#The name of the cookie to cache detected language or false|null to disable that feature
LANG_DETECTOR_COOKIE=locale

#A comma-separated list of available languages on application
LANG_DETECTOR_LANGUAGES="en,fr,pt_BR"

#To aliase the language use the notation ":", "=", ":=" or "=>" to separate the alias and its value.
# LANG_DETECTOR_LANGUAGES="en, en-us:en, pt-br:pt_BR"
```
Expand Down
27 changes: 21 additions & 6 deletions src/Middleware/InjectLanguageCookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
class InjectLanguageCookie
{
/**
* @param mixed $request
* @param mixed $request
* @param Closure $next
*
* @return mixed
Expand All @@ -20,11 +20,26 @@ public function handle($request, Closure $next)
/** @var \Illuminate\Http\Response $response */
$response = $next($request);

$cookie = cookie()->forever(
config('lang-detector.cookie_name', 'locale'),
app('translator')->getLocale()
);
if ($this->config('cookie', true)) {
$cookie = cookie()->forever(
$this->config('cookie_name', 'locale'),
app('translator')->getLocale()
);

return $response->withCookie($cookie);
$response->withCookie($cookie);
}

return $response;
}

/**
* @param string $key
* @param mixed $default
*
* @return mixed
*/
public function config($key, $default = null)
{
return config('lang-detector.'.$key, $default);
}
}
5 changes: 5 additions & 0 deletions src/Providers/LanguageDetectorServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ public function boot()
*/
public function register()
{
$this->app->resolving('Illuminate\Cookie\Middleware\EncryptCookies', function ($middleware) {
if ($this->config('cookie', true) && ! $this->config('cookie_encrypt', false)) {
$middleware->disableFor($this->config('lang-detector.cookie_name', 'locale'));
}
});
}

/**
Expand Down

0 comments on commit cb71dbc

Please sign in to comment.