language localization with browser settings

This commit is contained in:
Arno Kaimbacher 2018-11-27 12:21:42 +01:00
parent 8d91d0e7a8
commit f0e84a2991
13 changed files with 231 additions and 173 deletions

View File

@ -31,9 +31,9 @@ class LocalizationController extends Controller
//$lang = Input::get('language');
//Session::put('locale', $lang);
Session::put(['locale' => $lang]);
// Session::save();
// //Session::put('locale', $lang);
// Session::put(['locale' => $lang]);
//return redirect(url(URL::previous()));
return Redirect::back();

View File

@ -33,7 +33,7 @@ class Kernel extends HttpKernel
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\App\Http\Middleware\Locale::class,
\App\Http\Middleware\LocaleSessionRedirect::class,
],
'api' => [

View File

@ -0,0 +1,35 @@
<?php
namespace App\Http\Middleware;
class LaravelLocalizationMiddlewareBase
{
/**
* The URIs that should not be localized.
*
* @var array
*/
protected $except;
/**
* Determine if the request has a URI that should not be localized.
*
* @param \Illuminate\Http\Request $request
* @return bool
*/
protected function shouldIgnore($request)
{
$this->except = $this->except ?? config('laravellocalization.urlsIgnored', []);
foreach ($this->except as $except) {
if ($except !== '/') {
$except = trim($except, '/');
}
if ($request->is($except)) {
return true;
}
}
return false;
}
}

View File

@ -1,33 +0,0 @@
<?php
namespace App\Http\Middleware;
use Closure;
use Session;
use App;
use Config;
class Locale
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
// if(!Session::has('locale'))
// {
// Session::put('locale', Config::get('app.locale'));
// }
$language = Session::get('locale', Config::get('app.locale'));
// $data = Session::all();
// $language =Session::get('locale1');
App::setLocale($language);
return $next($request);
}
}

View File

@ -0,0 +1,62 @@
<?php
namespace App\Http\Middleware;
use Closure;
use Session;
use App;
use Config;
use Illuminate\Http\RedirectResponse;
use Mcamara\LaravelLocalization\LanguageNegotiator;
class LocaleSessionRedirect extends LaravelLocalizationMiddlewareBase
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
// if(!Session::has('locale'))
// {
// Session::put('locale', Config::get('app.locale'));
// }
// If the URL of the request is in exceptions.
if ($this->shouldIgnore($request)) {
return $next($request);
}
$params = explode('/', $request->path());//only on refresh 0:"pages"; 1: "imprint"
//$langParam = $request->input('lang', false);
$locale = Session::get('locale', false);
//old
//$locale = Session::get('locale', Config::get('app.locale'));
//$test = app('laravellocalization');
// if (\count($params) > 0 && app('laravellocalization')->checkLocaleInSupportedLocales(langParam)) {
if (\count($params) > 1 && app('laravellocalization')->checkLocaleInSupportedLocales($params[1])) {
//session(['locale' => $params[0]]);
Session::put('locale', $params[1]);
return $next($request);
} elseif (empty($locale) && app('laravellocalization')->hideUrlAndAcceptHeader()) {
// When default locale is hidden and accept language header is true,
// then compute browser language when no session has been set.
// Once the session has been set, there is no need
// to negotiate language from browser again.
$negotiator = new LanguageNegotiator(app('laravellocalization')->getDefaultLocale(), app('laravellocalization')->getSupportedLocales(), $request);
$locale = $negotiator->negotiateLanguage();
//session(['locale' => $locale]);
Session::put('locale', $params[0]);
}
if ($locale === false) {
$locale = app('laravellocalization')->getCurrentLocale();
}
App::setLocale($locale);
return $next($request);
}
}

View File

@ -31,5 +31,4 @@ class AppServiceProvider extends ServiceProvider
// 'App\Services\Registrar'
);
}
}

View File

@ -19,6 +19,4 @@ class BroadcastServiceProvider extends ServiceProvider
require base_path('routes/channels.php');
}
}

View File

@ -1,9 +1,10 @@
<?php namespace App\Providers;
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class ConfigServiceProvider extends ServiceProvider {
class ConfigServiceProvider extends ServiceProvider
{
/**
* Overwrite any vendor / package configuration.
*
@ -19,5 +20,4 @@ class ConfigServiceProvider extends ServiceProvider {
//
]);
}
}

View File

@ -1,10 +1,10 @@
<?php namespace App\Providers;
<?php
namespace App\Providers;
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
class EventServiceProvider extends ServiceProvider {
class EventServiceProvider extends ServiceProvider
{
/**
* The event handler mappings for the application.
*
@ -28,5 +28,4 @@ class EventServiceProvider extends ServiceProvider {
//
}
}

View File

@ -1,5 +1,4 @@
<?php
namespace App\Providers;
use Illuminate\Support\Facades\Route;
@ -73,5 +72,4 @@ class RouteServiceProvider extends ServiceProvider
->namespace($this->namespace)
->group(base_path('routes/api.php'));
}
}

150
composer.lock generated
View File

@ -908,16 +908,16 @@
},
{
"name": "league/flysystem",
"version": "1.0.48",
"version": "1.0.49",
"source": {
"type": "git",
"url": "https://github.com/thephpleague/flysystem.git",
"reference": "a6ded5b2f6055e2db97b4b859fdfca2b952b78aa"
"reference": "a63cc83d8a931b271be45148fa39ba7156782ffd"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/thephpleague/flysystem/zipball/a6ded5b2f6055e2db97b4b859fdfca2b952b78aa",
"reference": "a6ded5b2f6055e2db97b4b859fdfca2b952b78aa",
"url": "https://api.github.com/repos/thephpleague/flysystem/zipball/a63cc83d8a931b271be45148fa39ba7156782ffd",
"reference": "a63cc83d8a931b271be45148fa39ba7156782ffd",
"shasum": ""
},
"require": {
@ -988,7 +988,7 @@
"sftp",
"storage"
],
"time": "2018-10-15T13:53:10+00:00"
"time": "2018-11-23T23:41:29+00:00"
},
{
"name": "mcamara/laravel-localization",
@ -1176,16 +1176,16 @@
},
{
"name": "nesbot/carbon",
"version": "1.35.1",
"version": "1.36.1",
"source": {
"type": "git",
"url": "https://github.com/briannesbitt/Carbon.git",
"reference": "5c05a2be472b22f63291d192410df9f0e0de3b19"
"reference": "63da8cdf89d7a5efe43aabc794365f6e7b7b8983"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/briannesbitt/Carbon/zipball/5c05a2be472b22f63291d192410df9f0e0de3b19",
"reference": "5c05a2be472b22f63291d192410df9f0e0de3b19",
"url": "https://api.github.com/repos/briannesbitt/Carbon/zipball/63da8cdf89d7a5efe43aabc794365f6e7b7b8983",
"reference": "63da8cdf89d7a5efe43aabc794365f6e7b7b8983",
"shasum": ""
},
"require": {
@ -1230,7 +1230,7 @@
"datetime",
"time"
],
"time": "2018-11-14T21:55:58+00:00"
"time": "2018-11-22T18:23:02+00:00"
},
{
"name": "nikic/php-parser",
@ -1379,16 +1379,16 @@
},
{
"name": "psr/log",
"version": "1.0.2",
"version": "1.1.0",
"source": {
"type": "git",
"url": "https://github.com/php-fig/log.git",
"reference": "4ebe3a8bf773a19edfe0a84b6585ba3d401b724d"
"reference": "6c001f1daafa3a3ac1d8ff69ee4db8e799a654dd"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/php-fig/log/zipball/4ebe3a8bf773a19edfe0a84b6585ba3d401b724d",
"reference": "4ebe3a8bf773a19edfe0a84b6585ba3d401b724d",
"url": "https://api.github.com/repos/php-fig/log/zipball/6c001f1daafa3a3ac1d8ff69ee4db8e799a654dd",
"reference": "6c001f1daafa3a3ac1d8ff69ee4db8e799a654dd",
"shasum": ""
},
"require": {
@ -1422,7 +1422,7 @@
"psr",
"psr-3"
],
"time": "2016-10-10T12:19:37+00:00"
"time": "2018-11-20T15:27:04+00:00"
},
{
"name": "psr/simple-cache",
@ -1747,16 +1747,16 @@
},
{
"name": "symfony/console",
"version": "v3.4.18",
"version": "v3.4.19",
"source": {
"type": "git",
"url": "https://github.com/symfony/console.git",
"reference": "1d228fb4602047d7b26a0554e0d3efd567da5803"
"reference": "8f80fc39bbc3b7c47ee54ba7aa2653521ace94bb"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/console/zipball/1d228fb4602047d7b26a0554e0d3efd567da5803",
"reference": "1d228fb4602047d7b26a0554e0d3efd567da5803",
"url": "https://api.github.com/repos/symfony/console/zipball/8f80fc39bbc3b7c47ee54ba7aa2653521ace94bb",
"reference": "8f80fc39bbc3b7c47ee54ba7aa2653521ace94bb",
"shasum": ""
},
"require": {
@ -1812,20 +1812,20 @@
],
"description": "Symfony Console Component",
"homepage": "https://symfony.com",
"time": "2018-10-30T16:50:50+00:00"
"time": "2018-11-26T12:48:07+00:00"
},
{
"name": "symfony/css-selector",
"version": "v4.1.7",
"version": "v4.1.8",
"source": {
"type": "git",
"url": "https://github.com/symfony/css-selector.git",
"reference": "d67de79a70a27d93c92c47f37ece958bf8de4d8a"
"reference": "9e4dc57949853315561f0cd5eb84d0707465502a"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/css-selector/zipball/d67de79a70a27d93c92c47f37ece958bf8de4d8a",
"reference": "d67de79a70a27d93c92c47f37ece958bf8de4d8a",
"url": "https://api.github.com/repos/symfony/css-selector/zipball/9e4dc57949853315561f0cd5eb84d0707465502a",
"reference": "9e4dc57949853315561f0cd5eb84d0707465502a",
"shasum": ""
},
"require": {
@ -1865,20 +1865,20 @@
],
"description": "Symfony CssSelector Component",
"homepage": "https://symfony.com",
"time": "2018-10-02T16:36:10+00:00"
"time": "2018-11-11T19:51:29+00:00"
},
{
"name": "symfony/debug",
"version": "v3.4.18",
"version": "v3.4.19",
"source": {
"type": "git",
"url": "https://github.com/symfony/debug.git",
"reference": "fe9793af008b651c5441bdeab21ede8172dab097"
"reference": "2016b3eec2e49c127dd02d0ef44a35c53181560d"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/debug/zipball/fe9793af008b651c5441bdeab21ede8172dab097",
"reference": "fe9793af008b651c5441bdeab21ede8172dab097",
"url": "https://api.github.com/repos/symfony/debug/zipball/2016b3eec2e49c127dd02d0ef44a35c53181560d",
"reference": "2016b3eec2e49c127dd02d0ef44a35c53181560d",
"shasum": ""
},
"require": {
@ -1921,20 +1921,20 @@
],
"description": "Symfony Debug Component",
"homepage": "https://symfony.com",
"time": "2018-10-31T09:06:03+00:00"
"time": "2018-11-11T19:48:54+00:00"
},
{
"name": "symfony/event-dispatcher",
"version": "v3.4.18",
"version": "v3.4.19",
"source": {
"type": "git",
"url": "https://github.com/symfony/event-dispatcher.git",
"reference": "db9e829c8f34c3d35cf37fcd4cdb4293bc4a2f14"
"reference": "d365fc4416ec4980825873962ea5d1b1bca46f1a"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/event-dispatcher/zipball/db9e829c8f34c3d35cf37fcd4cdb4293bc4a2f14",
"reference": "db9e829c8f34c3d35cf37fcd4cdb4293bc4a2f14",
"url": "https://api.github.com/repos/symfony/event-dispatcher/zipball/d365fc4416ec4980825873962ea5d1b1bca46f1a",
"reference": "d365fc4416ec4980825873962ea5d1b1bca46f1a",
"shasum": ""
},
"require": {
@ -1984,20 +1984,20 @@
],
"description": "Symfony EventDispatcher Component",
"homepage": "https://symfony.com",
"time": "2018-10-30T16:50:50+00:00"
"time": "2018-11-26T10:17:44+00:00"
},
{
"name": "symfony/finder",
"version": "v3.4.18",
"version": "v3.4.19",
"source": {
"type": "git",
"url": "https://github.com/symfony/finder.git",
"reference": "54ba444dddc5bd5708a34bd095ea67c6eb54644d"
"reference": "6cf2be5cbd0e87aa35c01f80ae0bf40b6798e442"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/finder/zipball/54ba444dddc5bd5708a34bd095ea67c6eb54644d",
"reference": "54ba444dddc5bd5708a34bd095ea67c6eb54644d",
"url": "https://api.github.com/repos/symfony/finder/zipball/6cf2be5cbd0e87aa35c01f80ae0bf40b6798e442",
"reference": "6cf2be5cbd0e87aa35c01f80ae0bf40b6798e442",
"shasum": ""
},
"require": {
@ -2033,20 +2033,20 @@
],
"description": "Symfony Finder Component",
"homepage": "https://symfony.com",
"time": "2018-10-03T08:46:40+00:00"
"time": "2018-11-11T19:48:54+00:00"
},
{
"name": "symfony/http-foundation",
"version": "v3.4.18",
"version": "v3.4.19",
"source": {
"type": "git",
"url": "https://github.com/symfony/http-foundation.git",
"reference": "5aea7a86ca3203dd7a257e765b4b9c9cfd01c6c0"
"reference": "ea61dd57c4399b0b2a4162e1820cd9d0783acd38"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/http-foundation/zipball/5aea7a86ca3203dd7a257e765b4b9c9cfd01c6c0",
"reference": "5aea7a86ca3203dd7a257e765b4b9c9cfd01c6c0",
"url": "https://api.github.com/repos/symfony/http-foundation/zipball/ea61dd57c4399b0b2a4162e1820cd9d0783acd38",
"reference": "ea61dd57c4399b0b2a4162e1820cd9d0783acd38",
"shasum": ""
},
"require": {
@ -2087,20 +2087,20 @@
],
"description": "Symfony HttpFoundation Component",
"homepage": "https://symfony.com",
"time": "2018-10-31T08:57:11+00:00"
"time": "2018-11-26T10:17:44+00:00"
},
{
"name": "symfony/http-kernel",
"version": "v3.4.18",
"version": "v3.4.19",
"source": {
"type": "git",
"url": "https://github.com/symfony/http-kernel.git",
"reference": "4bf0be7c7fe63eff6a5eae2f21c83e77e31a56fb"
"reference": "78528325d90e5ad54a6e9eca750fe176932bc4fa"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/http-kernel/zipball/4bf0be7c7fe63eff6a5eae2f21c83e77e31a56fb",
"reference": "4bf0be7c7fe63eff6a5eae2f21c83e77e31a56fb",
"url": "https://api.github.com/repos/symfony/http-kernel/zipball/78528325d90e5ad54a6e9eca750fe176932bc4fa",
"reference": "78528325d90e5ad54a6e9eca750fe176932bc4fa",
"shasum": ""
},
"require": {
@ -2176,7 +2176,7 @@
],
"description": "Symfony HttpKernel Component",
"homepage": "https://symfony.com",
"time": "2018-11-03T10:03:02+00:00"
"time": "2018-11-26T14:04:48+00:00"
},
{
"name": "symfony/polyfill-ctype",
@ -2356,16 +2356,16 @@
},
{
"name": "symfony/process",
"version": "v3.4.18",
"version": "v3.4.19",
"source": {
"type": "git",
"url": "https://github.com/symfony/process.git",
"reference": "35c2914a9f50519bd207164c353ae4d59182c2cb"
"reference": "abb46b909dd6ba0b50e10d4c10ffe6ee96dd70f2"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/process/zipball/35c2914a9f50519bd207164c353ae4d59182c2cb",
"reference": "35c2914a9f50519bd207164c353ae4d59182c2cb",
"url": "https://api.github.com/repos/symfony/process/zipball/abb46b909dd6ba0b50e10d4c10ffe6ee96dd70f2",
"reference": "abb46b909dd6ba0b50e10d4c10ffe6ee96dd70f2",
"shasum": ""
},
"require": {
@ -2401,20 +2401,20 @@
],
"description": "Symfony Process Component",
"homepage": "https://symfony.com",
"time": "2018-10-14T17:33:21+00:00"
"time": "2018-11-20T16:10:26+00:00"
},
{
"name": "symfony/routing",
"version": "v3.4.18",
"version": "v3.4.19",
"source": {
"type": "git",
"url": "https://github.com/symfony/routing.git",
"reference": "585f6e2d740393d546978769dd56e496a6233e0b"
"reference": "86eb1a581279b5e40ca280a4f63a15e37d51d16c"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/routing/zipball/585f6e2d740393d546978769dd56e496a6233e0b",
"reference": "585f6e2d740393d546978769dd56e496a6233e0b",
"url": "https://api.github.com/repos/symfony/routing/zipball/86eb1a581279b5e40ca280a4f63a15e37d51d16c",
"reference": "86eb1a581279b5e40ca280a4f63a15e37d51d16c",
"shasum": ""
},
"require": {
@ -2478,20 +2478,20 @@
"uri",
"url"
],
"time": "2018-10-02T12:28:39+00:00"
"time": "2018-11-26T08:40:22+00:00"
},
{
"name": "symfony/translation",
"version": "v4.1.7",
"version": "v4.1.8",
"source": {
"type": "git",
"url": "https://github.com/symfony/translation.git",
"reference": "aa04dc1c75b7d3da7bd7003104cd0cfc5dff635c"
"reference": "615e3cf75d00a7d6788316d9631957991ba9c26a"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/translation/zipball/aa04dc1c75b7d3da7bd7003104cd0cfc5dff635c",
"reference": "aa04dc1c75b7d3da7bd7003104cd0cfc5dff635c",
"url": "https://api.github.com/repos/symfony/translation/zipball/615e3cf75d00a7d6788316d9631957991ba9c26a",
"reference": "615e3cf75d00a7d6788316d9631957991ba9c26a",
"shasum": ""
},
"require": {
@ -2547,20 +2547,20 @@
],
"description": "Symfony Translation Component",
"homepage": "https://symfony.com",
"time": "2018-10-28T18:38:52+00:00"
"time": "2018-11-26T10:26:29+00:00"
},
{
"name": "symfony/var-dumper",
"version": "v3.4.18",
"version": "v3.4.19",
"source": {
"type": "git",
"url": "https://github.com/symfony/var-dumper.git",
"reference": "ff8ac19e97e5c7c3979236b584719a1190f84181"
"reference": "6867713afe6c50ade2f34ed6435563b065a52145"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/var-dumper/zipball/ff8ac19e97e5c7c3979236b584719a1190f84181",
"reference": "ff8ac19e97e5c7c3979236b584719a1190f84181",
"url": "https://api.github.com/repos/symfony/var-dumper/zipball/6867713afe6c50ade2f34ed6435563b065a52145",
"reference": "6867713afe6c50ade2f34ed6435563b065a52145",
"shasum": ""
},
"require": {
@ -2616,7 +2616,7 @@
"debug",
"dump"
],
"time": "2018-10-02T16:33:53+00:00"
"time": "2018-11-20T16:10:26+00:00"
},
{
"name": "tijsverkoyen/css-to-inline-styles",
@ -2717,16 +2717,16 @@
},
{
"name": "yajra/laravel-datatables-oracle",
"version": "v8.11.0",
"version": "v8.13.1",
"source": {
"type": "git",
"url": "https://github.com/yajra/laravel-datatables.git",
"reference": "f9a9714918037c5b92645a7beb63370db0ecc172"
"reference": "3d7f05687069d90ca5dc3e75bf269e13eecccc76"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/yajra/laravel-datatables/zipball/f9a9714918037c5b92645a7beb63370db0ecc172",
"reference": "f9a9714918037c5b92645a7beb63370db0ecc172",
"url": "https://api.github.com/repos/yajra/laravel-datatables/zipball/3d7f05687069d90ca5dc3e75bf269e13eecccc76",
"reference": "3d7f05687069d90ca5dc3e75bf269e13eecccc76",
"shasum": ""
},
"require": {
@ -2784,7 +2784,7 @@
"jquery",
"laravel"
],
"time": "2018-11-20T04:34:22+00:00"
"time": "2018-11-23T08:05:22+00:00"
},
{
"name": "zizaco/entrust",

View File

@ -46,7 +46,7 @@ return [
//'en-AU' => ['name' => 'Australian English', 'script' => 'Latn', 'native' => 'Australian English', 'regional' => 'en_AU'],
//'en-GB' => ['name' => 'British English', 'script' => 'Latn', 'native' => 'British English', 'regional' => 'en_GB'],
//'en-US' => ['name' => 'U.S. English', 'script' => 'Latn', 'native' => 'U.S. English', 'regional' => 'en_US'],
'es' => ['name' => 'Spanish', 'script' => 'Latn', 'native' => 'español', 'regional' => 'es_ES'],
//'es' => ['name' => 'Spanish', 'script' => 'Latn', 'native' => 'español', 'regional' => 'es_ES'],
//'eo' => ['name' => 'Esperanto', 'script' => 'Latn', 'native' => 'esperanto', 'regional' => ''],
//'eu' => ['name' => 'Basque', 'script' => 'Latn', 'native' => 'euskara', 'regional' => 'eu_ES'],
//'ewo' => ['name' => 'Ewondo', 'script' => 'Latn', 'native' => 'ewondo', 'regional' => ''],
@ -323,6 +323,6 @@ return [
// URLs which should not be processed, e.g. '/nova', '/nova/*', '/nova-api/*' or specific application URLs
// Defaults to []
'urlsIgnored' => ['/skipped'],
'urlsIgnored' => ['/skipped', '/settings', '/settings/*'],
];