diff --git a/composer.json b/composer.json index da1a0aa..ca21f98 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/config/lang-detector.php b/config/lang-detector.php index e1112ff..52157d4 100644 --- a/config/lang-detector.php +++ b/config/lang-detector.php @@ -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 */ diff --git a/readme.md b/readme.md index 5eaac8f..f3e466c 100644 --- a/readme.md +++ b/readme.md @@ -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" ``` diff --git a/src/Middleware/InjectLanguageCookie.php b/src/Middleware/InjectLanguageCookie.php index b755c72..9fd7176 100644 --- a/src/Middleware/InjectLanguageCookie.php +++ b/src/Middleware/InjectLanguageCookie.php @@ -10,7 +10,7 @@ class InjectLanguageCookie { /** - * @param mixed $request + * @param mixed $request * @param Closure $next * * @return mixed @@ -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); } } diff --git a/src/Providers/LanguageDetectorServiceProvider.php b/src/Providers/LanguageDetectorServiceProvider.php index c626aa5..e637b86 100644 --- a/src/Providers/LanguageDetectorServiceProvider.php +++ b/src/Providers/LanguageDetectorServiceProvider.php @@ -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')); + } + }); } /**