Skip to content

Commit

Permalink
complete rewrtie
Browse files Browse the repository at this point in the history
  • Loading branch information
bezhanSalleh committed Nov 18, 2023
1 parent 1e38da0 commit 2dd5be8
Show file tree
Hide file tree
Showing 8 changed files with 329 additions and 138 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ charset = utf-8
indent_size = 4
indent_style = space
end_of_line = lf
insert_final_newline = true
insert_final_newline = false
trim_trailing_whitespace = true

[*.md]
Expand Down
2 changes: 1 addition & 1 deletion resources/dist/filament-language-switch.css

Large diffs are not rendered by default.

141 changes: 106 additions & 35 deletions resources/views/language-switch.blade.php
Original file line number Diff line number Diff line change
@@ -1,52 +1,123 @@
<x-filament::dropdown teleport placement="bottom-end" class="fi-dropdown fi-user-menu">
@php
$locales = $languageSwitch->getLocales();
@endphp
<x-filament::dropdown
teleport
placement="bottom"
:width="$languageSwitch->isFlagsOnly() ? 'flags-only' : null"
class="fi-dropdown fi-user-menu"
>
<style>
.filament-dropdown-list-item-label {
display: flex;
justify-content: flex-start;
align-items: center;
.flags-only {
max-width: 3rem!important;
}
</style>

<x-slot name="trigger" class="">
<div
class="flex items-center justify-center w-9 h-9 font-semibold text-sm text-white rounded-full language-switch-trigger bg-primary-500 dark:text-primary-500 dark:bg-gray-900 ring-1 ring-inset ring-gray-950/10 dark:ring-white/20">
{{ \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() }}
@class([
"flex items-center justify-center text-sm font-semibold w-9 h-9 language-switch-trigger text-primary-500 bg-primary-500/10",
"rounded-full" => $languageSwitch->isCircular(),
"rounded-lg" => !$languageSwitch->isCircular(),
"p-1 ring-2 ring-inset ring-gray-200 hover:ring-gray-300 dark:ring-gray-500 hover:dark:ring-gray-400" => $languageSwitch->isFlagsOnly(),
])
@if ($languageSwitch->isFlagsOnly())
x-tooltip="{
content: @js($languageSwitch->getLabel(app()->getLocale())),
theme: $store.theme,
placement: 'bottom'
}"
@endif
>
@if ($languageSwitch->isFlagsOnly())
<img
src="{{ $languageSwitch->getFlag(app()->getLocale()) }}"
@class([
"object-cover object-center max-w-none",
"rounded-full" => $languageSwitch->isCircular(),
"rounded-md" => !$languageSwitch->isCircular()
])
/>
@else
<span>{{ $languageSwitch->getFlag(app()->getLocale()) }}</span>
@endif
</div>
</x-slot>
<x-filament::dropdown.list class="!border-t-0">
@foreach (config('filament-language-switch.locales') as $key => $locale)
@if (!app()->isLocale($key))
<button type="button" class="fi-dropdown-list-item flex w-full items-center gap-2 whitespace-nowrap rounded-md p-2 text-sm transition-colors duration-75 outline-none disabled:pointer-events-none disabled:opacity-70 fi-dropdown-list-item-color-gray hover:bg-gray-950/5 focus:bg-gray-950/5 dark:hover:bg-white/5 dark:focus:bg-white/5" wire:click="changeLocale('{{ $key }}')">

@if (config('filament-language-switch.flag'))
<span>
<x-dynamic-component :component="'flag-1x1-' . (!blank($locale['flag_code']) ? $locale['flag_code'] : 'un')"
class="flex-shrink-0 w-5 h-5 group-hover:text-white group-focus:text-white text-primary-500"
style="border-radius: 0.25rem" />
</span>
<x-filament::dropdown.list
@class([
"!border-t-0 space-y-1",
])
>
@foreach ($locales as $locale)
@if (!app()->isLocale($locale))
<button
type="button"
wire:click="changeLocale('{{ $locale }}')"
@if ($languageSwitch->isFlagsOnly())
x-tooltip="{
content: @js($languageSwitch->getLabel($locale)),
theme: $store.theme,
placement: 'right'
}"
@endif
@class([
"flex items-center justify-center w-full px-2 py-0.5 text-sm transition-colors duration-75 rounded-md outline-none fi-dropdown-list-item whitespace-nowrap disabled:pointer-events-none disabled:opacity-70 fi-dropdown-list-item-color-gray hover:bg-gray-950/5 focus:bg-gray-950/5 dark:hover:bg-white/5 dark:focus:bg-white/5",
])
>

@if ($languageSwitch->isFlagsOnly())
<img
src="{{ $languageSwitch->getFlag($locale) }}"
@class([
"object-cover object-center max-w-none w-9 h-9 p-1 ring-2 ring-inset ring-gray-200 hover:ring-gray-300 dark:ring-gray-500 hover:dark:ring-gray-400",
"rounded-full" => $languageSwitch->isCircular(),
"rounded-lg" => !$languageSwitch->isCircular()
])
/>
@else

<span
class="w-6 h-6 flex items-center justify-center flex-shrink-0 @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-500 font-semibold rounded-full p-4 text-xs">
{{ \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('') }}
@class([
"flex items-center justify-center flex-shrink-0 w-6 h-6 p-4 text-xs font-semibold group-hover:bg-white group-hover:text-primary-600 group-hover:border group-hover:border-primary-500/10 group-focus:text-white bg-primary-500/10 text-primary-500",
"rounded-full" => $languageSwitch->isCircular(),
"rounded-lg" => !$languageSwitch->isCircular()
])
>
{{ $languageSwitch->getFlag($locale) }}
</span>
<span class="text-gray-700 hover:bg-transparent dark:text-gray-200">
{{ $languageSwitch->getLabel($locale) }}
</span>
@endif
<span class="hover:bg-transparent text-gray-700 dark:text-gray-200">
{{ \Illuminate\Support\Str::of($locale[config('filament-language-switch.native') ? 'native' : 'name'])->headline() }}
</span>
</button>
@endif
@endforeach

</x-filament::dropdown.list>
<script>
document.addEventListener('DOMContentLoaded', () => {
window.addEventListener('filament-language-changed', () => {
console.log('Language changed');
location.reload(true);
});
})
</script>
</x-filament::dropdown>
{{-- <x-filament::dropdown>
<x-slot name="trigger">
<x-filament::button>
More actions
</x-filament::button>
</x-slot>
<x-filament::dropdown.list>
<x-filament::dropdown.list.item
wire:click="openViewModal"
:image="$languageSwitch->getFlag(app()->getLocale())"
:tooltip="$languageSwitch->getLabel(app()->getLocale())"
>
{{ $languageSwitch->getLabel(app()->getLocale()) }}
</x-filament::dropdown.list.item>
<x-filament::dropdown.list.item wire:click="openEditModal" icon="heroicon-m-pencil">
Edit
</x-filament::dropdown.list.item>
<x-filament::dropdown.list.item wire:click="openDeleteModal">
Delete
</x-filament::dropdown.list.item>
</x-filament::dropdown.list>
</x-filament::dropdown> --}}
63 changes: 0 additions & 63 deletions src/FilamentLanguageSwitchPlugin.php

This file was deleted.

30 changes: 19 additions & 11 deletions src/FilamentLanguageSwitchServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

namespace BezhanSalleh\FilamentLanguageSwitch;

use BezhanSalleh\FilamentLanguageSwitch\Http\Middleware\SwitchLanguageLocale;
use Filament\Facades\Filament;
use Filament\Http\Middleware\DispatchServingFilamentEvent;
use Filament\Panel;
use Livewire\Livewire;
use Filament\Facades\Filament;
use Filament\Support\Assets\Css;
use Filament\Support\Facades\FilamentAsset;
use Spatie\LaravelPackageTools\Package;
use Filament\Support\Facades\FilamentAsset;
use Spatie\LaravelPackageTools\PackageServiceProvider;
use BezhanSalleh\FilamentLanguageSwitch\LanguageSwitch;
use Filament\Http\Middleware\DispatchServingFilamentEvent;
use BezhanSalleh\FilamentLanguageSwitch\Http\Livewire\FilamentLanguageSwitch;
use BezhanSalleh\FilamentLanguageSwitch\Http\Middleware\SwitchLanguageLocale;

class FilamentLanguageSwitchServiceProvider extends PackageServiceProvider
{
Expand All @@ -26,20 +29,25 @@ public function configurePackage(Package $package): void
public function packageBooted(): void
{
$this->registerPluginMiddleware();
}

public function registerPluginMiddleware(): void
{
collect(Filament::getPanels())
->filter(fn ($panel) => $panel->hasPlugin(static::$name))
->each(fn ($panel) => $this->reorderCurrentPanelMiddlewareStack($panel));
Livewire::component('filament-language-switch', FilamentLanguageSwitch::class);

FilamentAsset::register(
assets: [
Css::make('filament-language-switch', __DIR__ . '/../resources/dist/filament-language-switch.css'),
],
package: 'bezhansalleh/filament-language-switch'
);

Filament::serving(function () {
LanguageSwitch::boot();
});
}

public function registerPluginMiddleware(): void
{
collect(LanguageSwitch::make()->getPanels())
->each(fn ($panel) => $this->reorderCurrentPanelMiddlewareStack($panel));
}

protected function reorderCurrentPanelMiddlewareStack(Panel $panel): void
Expand All @@ -64,4 +72,4 @@ protected function reorderCurrentPanelMiddlewareStack(Panel $panel): void
->concat($middlewareCollection->slice($position))
->toArray();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

namespace BezhanSalleh\FilamentLanguageSwitch\Http\Livewire;

use Illuminate\Contracts\View\View;
use Livewire\Component;
use Illuminate\Contracts\View\View;
use BezhanSalleh\FilamentLanguageSwitch\LanguageSwitch;

class SwitchFilamentLanguage extends Component
class FilamentLanguageSwitch extends Component
{
public function changeLocale($locale)
{
Expand All @@ -21,6 +22,8 @@ public function changeLocale($locale)

public function render(): View
{
return view('filament-language-switch::language-switch');
return view('filament-language-switch::language-switch', [
'languageSwitch' => LanguageSwitch::make()
]);
}
}
36 changes: 12 additions & 24 deletions src/Http/Middleware/SwitchLanguageLocale.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,33 @@

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use BezhanSalleh\FilamentLanguageSwitch\LanguageSwitch;

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

if (array_key_exists($locale, config('filament-language-switch.locales'))) {
if (in_array($locale, LanguageSwitch::make()->getLocales())) {
app()->setLocale($locale);
}

return $next($request);
}

/**
* Determine the locale of the user's browser.
*
* @return ?string
*/
private function getBrowserLocale(Request $request): ?string
{
$userLangs = preg_split('/[,;]/', $request->server('HTTP_ACCEPT_LANGUAGE'));

foreach ($userLangs as $locale) {
if (Arr::exists(config('filament-language-switch.locales'), $locale)) {
return $locale;
}
return in_array($locale, LanguageSwitch::make()->getLocales())
? $locale
: null;
}

return null;
}
}
Loading

0 comments on commit 2dd5be8

Please sign in to comment.