language localization with browser settings
This commit is contained in:
parent
8d91d0e7a8
commit
f0e84a2991
13 changed files with 231 additions and 173 deletions
|
@ -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();
|
||||
|
|
|
@ -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' => [
|
||||
|
|
35
app/Http/Middleware/LaravelLocalizationMiddlewareBase.php
Normal file
35
app/Http/Middleware/LaravelLocalizationMiddlewareBase.php
Normal 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;
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
62
app/Http/Middleware/LocaleSessionRedirect.php
Normal file
62
app/Http/Middleware/LocaleSessionRedirect.php
Normal 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);
|
||||
}
|
||||
}
|
|
@ -31,5 +31,4 @@ class AppServiceProvider extends ServiceProvider
|
|||
// 'App\Services\Registrar'
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,24 +1,22 @@
|
|||
<?php
|
||||
<?php
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Illuminate\Support\Facades\Broadcast;
|
||||
|
||||
class BroadcastServiceProvider extends ServiceProvider
|
||||
class BroadcastServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Bootstrap any application services.
|
||||
*
|
||||
* @param \Illuminate\Bus\Dispatcher $dispatcher
|
||||
* @return void
|
||||
*/
|
||||
public function boot()
|
||||
{
|
||||
Broadcast::routes();
|
||||
|
||||
require base_path('routes/channels.php');
|
||||
}
|
||||
|
||||
/**
|
||||
* Bootstrap any application services.
|
||||
*
|
||||
* @param \Illuminate\Bus\Dispatcher $dispatcher
|
||||
* @return void
|
||||
*/
|
||||
public function boot()
|
||||
{
|
||||
Broadcast::routes();
|
||||
|
||||
require base_path('routes/channels.php');
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 {
|
|||
//
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,32 +1,31 @@
|
|||
<?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 {
|
||||
|
||||
/**
|
||||
* The event handler mappings for the application.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $listen = [
|
||||
class EventServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* The event handler mappings for the application.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $listen = [
|
||||
'App\Events\Event' => [
|
||||
'App\Listeners\EventListener',
|
||||
],
|
||||
];
|
||||
|
||||
/**
|
||||
* Register any other events for your application.
|
||||
*
|
||||
* @param \Illuminate\Contracts\Events\Dispatcher $events
|
||||
* @return void
|
||||
*/
|
||||
public function boot()
|
||||
{
|
||||
parent::boot();
|
||||
|
||||
//
|
||||
}
|
||||
/**
|
||||
* Register any other events for your application.
|
||||
*
|
||||
* @param \Illuminate\Contracts\Events\Dispatcher $events
|
||||
* @return void
|
||||
*/
|
||||
public function boot()
|
||||
{
|
||||
parent::boot();
|
||||
|
||||
//
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
<?php
|
||||
|
||||
<?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'));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -20,19 +20,19 @@ class SolariumServiceProvider extends ServiceProvider
|
|||
$this->app->bind(Client::class, function ($app) {
|
||||
// $config = config('solarium');
|
||||
$config = array(
|
||||
'endpoint' => array(
|
||||
'localhost' => array(
|
||||
'host' => '127.0.0.1',
|
||||
'port' => '8983',
|
||||
'path' => '/solr/',
|
||||
'core' => 'opus4'
|
||||
)
|
||||
'endpoint' => array(
|
||||
'localhost' => array(
|
||||
'host' => '127.0.0.1',
|
||||
'port' => '8983',
|
||||
'path' => '/solr/',
|
||||
'core' => 'opus4'
|
||||
)
|
||||
);
|
||||
)
|
||||
);
|
||||
//return new Client($config);
|
||||
return new Client($config);
|
||||
return new Client($config);
|
||||
//return new Client($app['config']['solarium']);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
public function provides()
|
||||
|
|
Loading…
Add table
editor.link_modal.header
Reference in a new issue