Skip to content

Commit

Permalink
fixes #20, #22 and #24
Browse files Browse the repository at this point in the history
  • Loading branch information
bezhanSalleh committed Apr 17, 2023
1 parent 8c26a37 commit b17a4a7
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 17 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
}
],
"require": {
"php": "^8.0|^8.1",
"php": "^8.0|^8.1|^8.2",
"filament/filament": "^2.0",
"spatie/laravel-package-tools": "^1.9.2",
"illuminate/contracts": "^8.0|^9.0|^10.0"
Expand Down
14 changes: 7 additions & 7 deletions resources/views/language-switch.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
'mr-4' => __('filament::layout.direction') === 'rtl',
])>
<div
class="language-switch-trigger flex items-center justify-center w-10 h-10 font-semibold text-white bg-center bg-cover rounded-full bg-primary-500 dark:bg-gray-900">
{{ str(app()->getLocale())->length() > 2
? str(app()->getLocale())->substr(0, 2)->upper()
: str(app()->getLocale())->upper() }}
class="flex items-center justify-center w-10 h-10 font-semibold text-white bg-center bg-cover rounded-full language-switch-trigger bg-primary-500 dark:bg-gray-900">
{{ \Illuminate\Support\Str::of(app()->getLocale())->length() > 2
? \Illuminate\Support\Str::of(app()->getLocale())->substr(0, 2)->upper()
: \Illuminate\Support\Str::of(app()->getLocale())->upper() }}
</div>
</x-slot>
<x-filament::dropdown.list class="">
Expand All @@ -31,13 +31,13 @@ class="flex-shrink-0 w-5 h-5 mr-4 rtl:ml-4 group-hover:text-white group-focus:te
@else
<span
class="w-6 h-6 flex items-center justify-center mr-4 flex-shrink-0 rtl:ml-4 @if (!app()->isLocale($key)) group-hover:bg-white group-hover:text-primary-600 group-hover:border group-hover:border-primary-500/10 group-focus:text-white @endif bg-primary-500/10 text-primary-600 font-semibold rounded-full p-1 text-xs">
{{ str($locale['name'])->snake()->upper()->explode('_')->map(function ($string) use ($locale) {
return str($locale['name'])->wordCount() > 1 ? str()->substr($string, 0, 1) : str()->substr($string, 0, 2);
{{ \Illuminate\Support\Str::of($locale['name'])->snake()->upper()->explode('_')->map(function ($string) use ($locale) {
return \Illuminate\Support\Str::of($locale['name'])->wordCount() > 1 ? \Illuminate\Support\Str::substr($string, 0, 1) : \Illuminate\Support\Str::substr($string, 0, 2);
})->take(2)->implode('') }}
</span>
@endif
<span class="hover:bg-transparent">
{{ str($locale[config('filament-language-switch.native') ? 'native' : 'name'])->headline() }}
{{ \Illuminate\Support\Str::of($locale[config('filament-language-switch.native') ? 'native' : 'name'])->headline() }}
</span>
</x-filament::dropdown.list.item>
@endif
Expand Down
22 changes: 15 additions & 7 deletions src/FilamentLanguageSwitchServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

namespace BezhanSalleh\FilamentLanguageSwitch;

use BezhanSalleh\FilamentLanguageSwitch\Http\Middleware\SwitchLanguageLocale;
use Filament\Facades\Filament;
use Filament\PluginServiceProvider;
use Spatie\LaravelPackageTools\Package;
use Filament\Http\Middleware\DispatchServingFilamentEvent;
use BezhanSalleh\FilamentLanguageSwitch\Http\Middleware\SwitchLanguageLocale;

class FilamentLanguageSwitchServiceProvider extends PluginServiceProvider
{
Expand All @@ -31,12 +32,19 @@ public function packageBooted(): void

public function registerSwitchLanguageMiddleware(): void
{
if (! array_key_exists(
$key = SwitchLanguageLocale::class,
$filamentMiddlewares = config('filament.middleware.base')
)) {
$filamentMiddlewares[] = $key;
config(['filament.middleware.base' => $filamentMiddlewares]);
$middlewareStack = config('filament.middleware.base');
$switchLanguageIndex = array_search(SwitchLanguageLocale::class, $middlewareStack);
$dispatchServingFilamentEventIndex = array_search(DispatchServingFilamentEvent::class, $middlewareStack);

if ($switchLanguageIndex === false || $switchLanguageIndex > $dispatchServingFilamentEventIndex) {

$middlewareStack = array_filter($middlewareStack, function ($middleware) {
return $middleware !== SwitchLanguageLocale::class;
});

array_splice($middlewareStack, $dispatchServingFilamentEventIndex, 0, [SwitchLanguageLocale::class]);

config(['filament.middleware.base' => $middlewareStack]);
}
}
}
3 changes: 3 additions & 0 deletions src/Http/Livewire/SwitchFilamentLanguage.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace BezhanSalleh\FilamentLanguageSwitch\Http\Livewire;

use Illuminate\Contracts\View\View;
use Illuminate\Support\Str;
use Livewire\Component;

class SwitchFilamentLanguage extends Component
Expand All @@ -11,6 +12,8 @@ public function changeLocale($locale)
{
session()->put('locale', $locale);

cookie()->queue(cookie()->forever('filament_language_switch_locale', $locale));

$this->redirect(request()->header('Referer'));
}

Expand Down
4 changes: 2 additions & 2 deletions src/Http/Middleware/SwitchLanguageLocale.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

class SwitchLanguageLocale
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
*/
public function handle(Request $request, Closure $next)
{
$locale = session()->get('locale') ?? $request->get('locale') ?? config('app.locale', 'en');
$locale = session()->get('locale') ?? $request->get('locale') ?? $request->cookie('filament_language_switch_locale') ?? config('app.locale', 'en');

if (array_key_exists($locale, config('filament-language-switch.locales'))) {
app()->setLocale($locale);
Expand Down

0 comments on commit b17a4a7

Please sign in to comment.