From effed0925808a09ca1a2fbc781b6c259eaa36268 Mon Sep 17 00:00:00 2001 From: Bezhan Salleh Date: Thu, 25 Jan 2024 04:36:12 +0430 Subject: [PATCH 1/7] wip --- src/Http/Middleware/SwitchLanguageLocale.php | 27 ++------------ src/LanguageSwitch.php | 37 ++++++++++++++++++++ 2 files changed, 40 insertions(+), 24 deletions(-) diff --git a/src/Http/Middleware/SwitchLanguageLocale.php b/src/Http/Middleware/SwitchLanguageLocale.php index 5ea7c08..bebdbe3 100644 --- a/src/Http/Middleware/SwitchLanguageLocale.php +++ b/src/Http/Middleware/SwitchLanguageLocale.php @@ -10,31 +10,10 @@ class SwitchLanguageLocale { 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'); - - if (in_array($locale, LanguageSwitch::make()->getLocales())) { - app()->setLocale($locale); - } + app()->setLocale( + LanguageSwitch::make()->getPreferredLocale() + ); return $next($request); } - - private function getBrowserLocale(Request $request): ?string - { - $appLocales = LanguageSwitch::make()->getLocales(); - - $userLocales = preg_split('/[,;]/', $request->server('HTTP_ACCEPT_LANGUAGE')); - - foreach ($userLocales as $locale) { - if (in_array($locale, $appLocales)) { - return $locale; - } - } - - return null; - } } diff --git a/src/LanguageSwitch.php b/src/LanguageSwitch.php index f72f045..1547c6a 100755 --- a/src/LanguageSwitch.php +++ b/src/LanguageSwitch.php @@ -38,6 +38,11 @@ class LanguageSwitch extends Component protected Closure | string $renderHook = 'panels::global-search.after'; + protected Closure | string | null $userPreferredLocale = null; + + protected Closure | string | null $preferredLocale = null; + + public static function make(): static { $static = app(static::class); @@ -146,6 +151,20 @@ public function renderHook(string $hook): static return $this; } + public function userPreferredLocale(Closure | string | null $locale): static + { + $this->userPreferredLocale = $locale; + + return $this; + } + + public function preferredLocale(Closure | string $locale): static + { + $this->preferredLocale = $locale; + + return $this; + } + public function visible(bool | Closure $insidePanels = true, bool | Closure $outsidePanels = false): static { $this->visibleInsidePanels = $insidePanels; @@ -229,6 +248,24 @@ public function getRenderHook(): string return (string) $this->evaluate($this->renderHook); } + public function getUserPreferredLocale(): ?string + { + return $this->evaluate($this->userPreferredLocale) ?? null; + } + + public function getPreferredLocale(): string + { + $locale = $this->perferredLocale ?? + session()->get('locale') ?? + request()->get('locale') ?? + request()->cookie('filament_language_switch_locale') ?? + $this->getUserPreferredLocale() ?? + config('app.locale', 'en') ?? + request()->getPreferredLanguage(); + + return in_array($locale, $this->getLocales(), true) ? $locale : config('app.locale'); + } + /** * @return array */ From bb4212bfd86d959c8b84ce03d92316cdf604221d Mon Sep 17 00:00:00 2001 From: bezhanSalleh Date: Thu, 25 Jan 2024 00:06:51 +0000 Subject: [PATCH 2/7] Fix styling --- src/LanguageSwitch.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/LanguageSwitch.php b/src/LanguageSwitch.php index 1547c6a..75bb34b 100755 --- a/src/LanguageSwitch.php +++ b/src/LanguageSwitch.php @@ -42,7 +42,6 @@ class LanguageSwitch extends Component protected Closure | string | null $preferredLocale = null; - public static function make(): static { $static = app(static::class); From 1426d0e81661d660a747153c7b74898198cff89b Mon Sep 17 00:00:00 2001 From: Bezhan Salleh Date: Thu, 25 Jan 2024 07:25:59 +0430 Subject: [PATCH 3/7] handling user preferred locale --- src/Http/Livewire/FilamentLanguageSwitch.php | 14 ++++------- src/LanguageSwitch.php | 25 ++++++++++---------- 2 files changed, 17 insertions(+), 22 deletions(-) diff --git a/src/Http/Livewire/FilamentLanguageSwitch.php b/src/Http/Livewire/FilamentLanguageSwitch.php index 1c10bf1..408d315 100644 --- a/src/Http/Livewire/FilamentLanguageSwitch.php +++ b/src/Http/Livewire/FilamentLanguageSwitch.php @@ -2,23 +2,17 @@ namespace BezhanSalleh\FilamentLanguageSwitch\Http\Livewire; -use BezhanSalleh\FilamentLanguageSwitch\Events\LocaleChanged; +use BezhanSalleh\FilamentLanguageSwitch\LanguageSwitch; use Illuminate\Contracts\View\View; +use Livewire\Attributes\On; use Livewire\Component; class FilamentLanguageSwitch extends Component { + #[On('language-switched')] public function changeLocale($locale) { - session()->put('locale', $locale); - - cookie()->queue(cookie()->forever('filament_language_switch_locale', $locale)); - - $this->dispatch('filament-language-changed'); - - event(new LocaleChanged($locale)); - - $this->redirect(request()->header('Referer')); + LanguageSwitch::trigger($locale); } diff --git a/src/LanguageSwitch.php b/src/LanguageSwitch.php index 1547c6a..e1c2dd9 100755 --- a/src/LanguageSwitch.php +++ b/src/LanguageSwitch.php @@ -5,6 +5,7 @@ namespace BezhanSalleh\FilamentLanguageSwitch; use BezhanSalleh\FilamentLanguageSwitch\Enums\Placement; +use BezhanSalleh\FilamentLanguageSwitch\Events\LocaleChanged; use Closure; use Exception; use Filament\Panel; @@ -40,9 +41,6 @@ class LanguageSwitch extends Component protected Closure | string | null $userPreferredLocale = null; - protected Closure | string | null $preferredLocale = null; - - public static function make(): static { $static = app(static::class); @@ -158,13 +156,6 @@ public function userPreferredLocale(Closure | string | null $locale): static return $this; } - public function preferredLocale(Closure | string $locale): static - { - $this->preferredLocale = $locale; - - return $this; - } - public function visible(bool | Closure $insidePanels = true, bool | Closure $outsidePanels = false): static { $this->visibleInsidePanels = $insidePanels; @@ -255,8 +246,7 @@ public function getUserPreferredLocale(): ?string public function getPreferredLocale(): string { - $locale = $this->perferredLocale ?? - session()->get('locale') ?? + $locale = session()->get('locale') ?? request()->get('locale') ?? request()->cookie('filament_language_switch_locale') ?? $this->getUserPreferredLocale() ?? @@ -304,4 +294,15 @@ public function getCharAvatar(string $locale): string ? str($locale)->substr(0, 2)->upper()->toString() : str($locale)->upper()->toString(); } + + public static function trigger($locale) + { + session()->put('locale', $locale); + + cookie()->queue(cookie()->forever('filament_language_switch_locale', $locale)); + + event(new LocaleChanged($locale)); + + return redirect(request()->header('Referer')); + } } From c41c1a98f6ddd5dc3c1029fb75ebdeaff0be9ad6 Mon Sep 17 00:00:00 2001 From: bezhanSalleh Date: Mon, 1 Apr 2024 16:27:22 +0000 Subject: [PATCH 4/7] Fix styling --- src/Http/Livewire/FilamentLanguageSwitch.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Http/Livewire/FilamentLanguageSwitch.php b/src/Http/Livewire/FilamentLanguageSwitch.php index 408d315..8a46b1b 100644 --- a/src/Http/Livewire/FilamentLanguageSwitch.php +++ b/src/Http/Livewire/FilamentLanguageSwitch.php @@ -4,7 +4,7 @@ use BezhanSalleh\FilamentLanguageSwitch\LanguageSwitch; use Illuminate\Contracts\View\View; -use Livewire\Attributes\On; +use Livewire\Attributes\On; use Livewire\Component; class FilamentLanguageSwitch extends Component From 2fde00e0aed4bea8eaf7d60de97ff609150e6d4c Mon Sep 17 00:00:00 2001 From: Bezhan Salleh Date: Mon, 1 Apr 2024 22:40:04 +0430 Subject: [PATCH 5/7] wip semi final --- resources/dist/filament-language-switch.css | 2 +- resources/views/components/flag.blade.php | 6 +--- resources/views/language-switch.blade.php | 2 +- resources/views/switch.blade.php | 2 +- src/Http/Livewire/FilamentLanguageSwitch.php | 5 ++- src/Http/Middleware/SwitchLanguageLocale.php | 2 +- src/LanguageSwitch.php | 35 ++++++++++++++++---- 7 files changed, 36 insertions(+), 18 deletions(-) diff --git a/resources/dist/filament-language-switch.css b/resources/dist/filament-language-switch.css index 1e4a250..b230d31 100644 --- a/resources/dist/filament-language-switch.css +++ b/resources/dist/filament-language-switch.css @@ -1 +1 @@ -.flex-shrink-0{flex-shrink:0}.space-x-2>:not([hidden])~:not([hidden]){--tw-space-x-reverse:0;margin-right:calc(.5rem*var(--tw-space-x-reverse));margin-left:calc(.5rem*(1 - var(--tw-space-x-reverse)))}.bg-primary-500\/10{background-color:rgba(var(--primary-500),.1)}.\!p-2{padding:.5rem!important}.\!p-2\.5{padding:.625rem!important}.text-gray-600,.text-primary-600{--tw-text-opacity:1}.flags-only{max-width:3rem!important}.fls-dropdown-width{max-width:-moz-fit-content!important;max-width:fit-content!important}/*! tailwindcss v3.3.3 | MIT License | https://tailwindcss.com*/*,:after,:before{box-sizing:border-box;border-width:0;border-style:solid;border-color:rgba(var(--gray-200),1)}:after,:before{--tw-content:""}html{line-height:1.5;-webkit-text-size-adjust:100%;-moz-tab-size:4;-o-tab-size:4;tab-size:4;font-family:var(--font-family),ui-sans-serif,system-ui,-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Arial,"Noto Sans",sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Noto Color Emoji";font-feature-settings:normal;font-variation-settings:normal}body{margin:0;line-height:inherit}hr{height:0;color:inherit;border-top-width:1px}abbr:where([title]){-webkit-text-decoration:underline dotted;text-decoration:underline dotted}h1,h2,h3,h4,h5,h6{font-size:inherit;font-weight:inherit}a{color:inherit;text-decoration:inherit}b,strong{font-weight:bolder}code,kbd,pre,samp{font-family:ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,Liberation Mono,Courier New,monospace;font-size:1em}small{font-size:80%}sub,sup{font-size:75%;line-height:0;position:relative;vertical-align:initial}sub{bottom:-.25em}sup{top:-.5em}table{text-indent:0;border-color:inherit;border-collapse:collapse}button,input,optgroup,select,textarea{font-family:inherit;font-feature-settings:inherit;font-variation-settings:inherit;font-size:100%;font-weight:inherit;line-height:inherit;color:inherit;margin:0;padding:0}button,select{text-transform:none}[type=button],[type=reset],[type=submit],button{-webkit-appearance:button;background-color:initial;background-image:none}:-moz-focusring{outline:auto}:-moz-ui-invalid{box-shadow:none}progress{vertical-align:initial}::-webkit-inner-spin-button,::-webkit-outer-spin-button{height:auto}[type=search]{-webkit-appearance:textfield;outline-offset:-2px}::-webkit-search-decoration{-webkit-appearance:none}::-webkit-file-upload-button{-webkit-appearance:button;font:inherit}summary{display:list-item}blockquote,dd,dl,figure,h1,h2,h3,h4,h5,h6,hr,p,pre{margin:0}fieldset{margin:0}fieldset,legend{padding:0}menu,ol,ul{list-style:none;margin:0;padding:0}dialog{padding:0}textarea{resize:vertical}input::-moz-placeholder,textarea::-moz-placeholder{opacity:1;color:rgba(var(--gray-400),1)}input::placeholder,textarea::placeholder{opacity:1;color:rgba(var(--gray-400),1)}[role=button],button{cursor:pointer}:disabled{cursor:default}audio,canvas,embed,iframe,img,object,svg,video{display:block;vertical-align:middle}img,video{max-width:100%;height:auto}[hidden]{display:none}[multiple],[type=date],[type=datetime-local],[type=email],[type=month],[type=number],[type=password],[type=search],[type=tel],[type=text],[type=time],[type=url],[type=week],input:where(:not([type])),select,textarea{-webkit-appearance:none;-moz-appearance:none;appearance:none;background-color:#fff;border-color:rgba(var(--gray-500),var(--tw-border-opacity,1));border-width:1px;border-radius:0;padding:.5rem .75rem;font-size:1rem;line-height:1.5rem;--tw-shadow:0 0 #0000}[multiple]:focus,[type=date]:focus,[type=datetime-local]:focus,[type=email]:focus,[type=month]:focus,[type=number]:focus,[type=password]:focus,[type=search]:focus,[type=tel]:focus,[type=text]:focus,[type=time]:focus,[type=url]:focus,[type=week]:focus,input:where(:not([type])):focus,select:focus,textarea:focus{outline:2px solid #0000;outline-offset:2px;--tw-ring-inset:var(--tw-empty,/*!*/ /*!*/);--tw-ring-offset-width:0px;--tw-ring-offset-color:#fff;--tw-ring-color:#2563eb;--tw-ring-offset-shadow:var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);--tw-ring-shadow:var(--tw-ring-inset) 0 0 0 calc(1px + var(--tw-ring-offset-width)) var(--tw-ring-color);box-shadow:var(--tw-ring-offset-shadow),var(--tw-ring-shadow),var(--tw-shadow);border-color:#2563eb}input::-moz-placeholder,textarea::-moz-placeholder{color:rgba(var(--gray-500),var(--tw-text-opacity,1));opacity:1}input::placeholder,textarea::placeholder{color:rgba(var(--gray-500),var(--tw-text-opacity,1));opacity:1}::-webkit-datetime-edit-fields-wrapper{padding:0}::-webkit-date-and-time-value{min-height:1.5em}::-webkit-datetime-edit,::-webkit-datetime-edit-day-field,::-webkit-datetime-edit-hour-field,::-webkit-datetime-edit-meridiem-field,::-webkit-datetime-edit-millisecond-field,::-webkit-datetime-edit-minute-field,::-webkit-datetime-edit-month-field,::-webkit-datetime-edit-second-field,::-webkit-datetime-edit-year-field{padding-top:0;padding-bottom:0}select{background-image:url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 20 20'%3E%3Cpath stroke='rgba(var(--gray-500), var(--tw-stroke-opacity, 1))' stroke-linecap='round' stroke-linejoin='round' stroke-width='1.5' d='m6 8 4 4 4-4'/%3E%3C/svg%3E");background-position:right .5rem center;background-repeat:no-repeat;background-size:1.5em 1.5em;padding-right:2.5rem;-webkit-print-color-adjust:exact;print-color-adjust:exact}[multiple],[size]:where(select:not([size="1"])){background-image:none;background-position:0 0;background-repeat:unset;background-size:initial;padding-right:.75rem;-webkit-print-color-adjust:unset;print-color-adjust:unset}[type=checkbox],[type=radio]{-webkit-appearance:none;-moz-appearance:none;appearance:none;padding:0;-webkit-print-color-adjust:exact;print-color-adjust:exact;display:inline-block;vertical-align:middle;background-origin:border-box;-webkit-user-select:none;-moz-user-select:none;user-select:none;flex-shrink:0;height:1rem;width:1rem;color:#2563eb;background-color:#fff;border-color:rgba(var(--gray-500),var(--tw-border-opacity,1));border-width:1px;--tw-shadow:0 0 #0000}[type=checkbox]{border-radius:0}[type=radio]{border-radius:100%}[type=checkbox]:focus,[type=radio]:focus{outline:2px solid #0000;outline-offset:2px;--tw-ring-inset:var(--tw-empty,/*!*/ /*!*/);--tw-ring-offset-width:2px;--tw-ring-offset-color:#fff;--tw-ring-color:#2563eb;--tw-ring-offset-shadow:var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);--tw-ring-shadow:var(--tw-ring-inset) 0 0 0 calc(2px + var(--tw-ring-offset-width)) var(--tw-ring-color);box-shadow:var(--tw-ring-offset-shadow),var(--tw-ring-shadow),var(--tw-shadow)}[type=checkbox]:checked,[type=radio]:checked{border-color:#0000;background-color:currentColor;background-size:100% 100%;background-position:50%;background-repeat:no-repeat}[type=checkbox]:checked{background-image:url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='%23fff' viewBox='0 0 16 16'%3E%3Cpath d='M12.207 4.793a1 1 0 0 1 0 1.414l-5 5a1 1 0 0 1-1.414 0l-2-2a1 1 0 0 1 1.414-1.414L6.5 9.086l4.293-4.293a1 1 0 0 1 1.414 0z'/%3E%3C/svg%3E")}[type=radio]:checked{background-image:url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='%23fff' viewBox='0 0 16 16'%3E%3Ccircle cx='8' cy='8' r='3'/%3E%3C/svg%3E")}[type=checkbox]:checked:focus,[type=checkbox]:checked:hover,[type=checkbox]:indeterminate,[type=radio]:checked:focus,[type=radio]:checked:hover{border-color:#0000;background-color:currentColor}[type=checkbox]:indeterminate{background-image:url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 16 16'%3E%3Cpath stroke='%23fff' stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='M4 8h8'/%3E%3C/svg%3E");background-size:100% 100%;background-position:50%;background-repeat:no-repeat}[type=checkbox]:indeterminate:focus,[type=checkbox]:indeterminate:hover{border-color:#0000;background-color:currentColor}[type=file]{background:unset;border-color:inherit;border-width:0;border-radius:0;padding:0;font-size:unset;line-height:inherit}[type=file]:focus{outline:1px solid ButtonText;outline:1px auto -webkit-focus-ring-color}*,::backdrop,:after,:before{--tw-border-spacing-x:0;--tw-border-spacing-y:0;--tw-translate-x:0;--tw-translate-y:0;--tw-rotate:0;--tw-skew-x:0;--tw-skew-y:0;--tw-scale-x:1;--tw-scale-y:1;--tw-pan-x: ;--tw-pan-y: ;--tw-pinch-zoom: ;--tw-scroll-snap-strictness:proximity;--tw-gradient-from-position: ;--tw-gradient-via-position: ;--tw-gradient-to-position: ;--tw-ordinal: ;--tw-slashed-zero: ;--tw-numeric-figure: ;--tw-numeric-spacing: ;--tw-numeric-fraction: ;--tw-ring-inset: ;--tw-ring-offset-width:0px;--tw-ring-offset-color:#fff;--tw-ring-color:#3b82f680;--tw-ring-offset-shadow:0 0 #0000;--tw-ring-shadow:0 0 #0000;--tw-shadow:0 0 #0000;--tw-shadow-colored:0 0 #0000;--tw-blur: ;--tw-brightness: ;--tw-contrast: ;--tw-grayscale: ;--tw-hue-rotate: ;--tw-invert: ;--tw-saturate: ;--tw-sepia: ;--tw-drop-shadow: ;--tw-backdrop-blur: ;--tw-backdrop-brightness: ;--tw-backdrop-contrast: ;--tw-backdrop-grayscale: ;--tw-backdrop-hue-rotate: ;--tw-backdrop-invert: ;--tw-backdrop-opacity: ;--tw-backdrop-saturate: ;--tw-backdrop-sepia: }.hover\:bg-gray-950\/5:hover{background-color:rgba(var(--gray-950),.05)}.hover\:bg-transparent:hover{background-color:initial}.hover\:ring-gray-300:hover{--tw-ring-opacity:1;--tw-ring-color:rgba(var(--gray-300),var(--tw-ring-opacity))}.focus\:bg-gray-950\/5:focus{background-color:rgba(var(--gray-950),.05)}.group:hover .group-hover\:border{border-width:1px}.group:hover .group-hover\:border-primary-500\/10{border-color:rgba(var(--primary-500),.1)}.group:hover .group-hover\:bg-white{--tw-bg-opacity:1;background-color:rgb(255 255 255/var(--tw-bg-opacity))}.group:hover .group-hover\:text-primary-600{--tw-text-opacity:1;color:rgba(var(--primary-600),var(--tw-text-opacity))}.group:focus .group-focus\:text-white{--tw-text-opacity:1;color:rgb(255 255 255/var(--tw-text-opacity))}:is([dir=rtl] .rtl\:space-x-reverse)>:not([hidden])~:not([hidden]){--tw-space-x-reverse:1}:is(.dark .dark\:bg-gray-950){--tw-bg-opacity:1;background-color:rgba(var(--gray-950),var(--tw-bg-opacity))}:is(.dark .dark\:text-gray-200){--tw-text-opacity:1;color:rgba(var(--gray-200),var(--tw-text-opacity))}:is(.dark .dark\:ring-gray-500){--tw-ring-opacity:1;--tw-ring-color:rgba(var(--gray-500),var(--tw-ring-opacity))}:is(.dark .dark\:hover\:bg-white\/5:hover){background-color:#ffffff0d}:is(.dark .hover\:dark\:ring-gray-400):hover{--tw-ring-opacity:1;--tw-ring-color:rgba(var(--gray-400),var(--tw-ring-opacity))}:is(.dark .dark\:focus\:bg-white\/5:focus){background-color:#ffffff0d} \ No newline at end of file +.w-fit{width:-moz-fit-content;width:fit-content}.flex-shrink-0{flex-shrink:0}.space-x-2>:not([hidden])~:not([hidden]){--tw-space-x-reverse:0;margin-right:calc(.5rem*var(--tw-space-x-reverse));margin-left:calc(.5rem*(1 - var(--tw-space-x-reverse)))}.bg-primary-500\/10{background-color:rgba(var(--primary-500),.1)}.\!p-2{padding:.5rem!important}.\!p-2\.5{padding:.625rem!important}.text-gray-600,.text-primary-600{--tw-text-opacity:1}.flags-only{max-width:3rem!important}.fls-dropdown-width{max-width:-moz-fit-content!important;max-width:fit-content!important}/*! tailwindcss v3.3.3 | MIT License | https://tailwindcss.com*/*,:after,:before{box-sizing:border-box;border-width:0;border-style:solid;border-color:rgba(var(--gray-200),1)}:after,:before{--tw-content:""}html{line-height:1.5;-webkit-text-size-adjust:100%;-moz-tab-size:4;-o-tab-size:4;tab-size:4;font-family:var(--font-family),ui-sans-serif,system-ui,-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Arial,"Noto Sans",sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Noto Color Emoji";font-feature-settings:normal;font-variation-settings:normal}body{margin:0;line-height:inherit}hr{height:0;color:inherit;border-top-width:1px}abbr:where([title]){-webkit-text-decoration:underline dotted;text-decoration:underline dotted}h1,h2,h3,h4,h5,h6{font-size:inherit;font-weight:inherit}a{color:inherit;text-decoration:inherit}b,strong{font-weight:bolder}code,kbd,pre,samp{font-family:ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,Liberation Mono,Courier New,monospace;font-size:1em}small{font-size:80%}sub,sup{font-size:75%;line-height:0;position:relative;vertical-align:initial}sub{bottom:-.25em}sup{top:-.5em}table{text-indent:0;border-color:inherit;border-collapse:collapse}button,input,optgroup,select,textarea{font-family:inherit;font-feature-settings:inherit;font-variation-settings:inherit;font-size:100%;font-weight:inherit;line-height:inherit;color:inherit;margin:0;padding:0}button,select{text-transform:none}[type=button],[type=reset],[type=submit],button{-webkit-appearance:button;background-color:initial;background-image:none}:-moz-focusring{outline:auto}:-moz-ui-invalid{box-shadow:none}progress{vertical-align:initial}::-webkit-inner-spin-button,::-webkit-outer-spin-button{height:auto}[type=search]{-webkit-appearance:textfield;outline-offset:-2px}::-webkit-search-decoration{-webkit-appearance:none}::-webkit-file-upload-button{-webkit-appearance:button;font:inherit}summary{display:list-item}blockquote,dd,dl,figure,h1,h2,h3,h4,h5,h6,hr,p,pre{margin:0}fieldset{margin:0}fieldset,legend{padding:0}menu,ol,ul{list-style:none;margin:0;padding:0}dialog{padding:0}textarea{resize:vertical}input::-moz-placeholder,textarea::-moz-placeholder{opacity:1;color:rgba(var(--gray-400),1)}input::placeholder,textarea::placeholder{opacity:1;color:rgba(var(--gray-400),1)}[role=button],button{cursor:pointer}:disabled{cursor:default}audio,canvas,embed,iframe,img,object,svg,video{display:block;vertical-align:middle}img,video{max-width:100%;height:auto}[hidden]{display:none}[multiple],[type=date],[type=datetime-local],[type=email],[type=month],[type=number],[type=password],[type=search],[type=tel],[type=text],[type=time],[type=url],[type=week],input:where(:not([type])),select,textarea{-webkit-appearance:none;-moz-appearance:none;appearance:none;background-color:#fff;border-color:rgba(var(--gray-500),var(--tw-border-opacity,1));border-width:1px;border-radius:0;padding:.5rem .75rem;font-size:1rem;line-height:1.5rem;--tw-shadow:0 0 #0000}[multiple]:focus,[type=date]:focus,[type=datetime-local]:focus,[type=email]:focus,[type=month]:focus,[type=number]:focus,[type=password]:focus,[type=search]:focus,[type=tel]:focus,[type=text]:focus,[type=time]:focus,[type=url]:focus,[type=week]:focus,input:where(:not([type])):focus,select:focus,textarea:focus{outline:2px solid #0000;outline-offset:2px;--tw-ring-inset:var(--tw-empty,/*!*/ /*!*/);--tw-ring-offset-width:0px;--tw-ring-offset-color:#fff;--tw-ring-color:#2563eb;--tw-ring-offset-shadow:var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);--tw-ring-shadow:var(--tw-ring-inset) 0 0 0 calc(1px + var(--tw-ring-offset-width)) var(--tw-ring-color);box-shadow:var(--tw-ring-offset-shadow),var(--tw-ring-shadow),var(--tw-shadow);border-color:#2563eb}input::-moz-placeholder,textarea::-moz-placeholder{color:rgba(var(--gray-500),var(--tw-text-opacity,1));opacity:1}input::placeholder,textarea::placeholder{color:rgba(var(--gray-500),var(--tw-text-opacity,1));opacity:1}::-webkit-datetime-edit-fields-wrapper{padding:0}::-webkit-date-and-time-value{min-height:1.5em}::-webkit-datetime-edit,::-webkit-datetime-edit-day-field,::-webkit-datetime-edit-hour-field,::-webkit-datetime-edit-meridiem-field,::-webkit-datetime-edit-millisecond-field,::-webkit-datetime-edit-minute-field,::-webkit-datetime-edit-month-field,::-webkit-datetime-edit-second-field,::-webkit-datetime-edit-year-field{padding-top:0;padding-bottom:0}select{background-image:url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 20 20'%3E%3Cpath stroke='rgba(var(--gray-500), var(--tw-stroke-opacity, 1))' stroke-linecap='round' stroke-linejoin='round' stroke-width='1.5' d='m6 8 4 4 4-4'/%3E%3C/svg%3E");background-position:right .5rem center;background-repeat:no-repeat;background-size:1.5em 1.5em;padding-right:2.5rem;-webkit-print-color-adjust:exact;print-color-adjust:exact}[multiple],[size]:where(select:not([size="1"])){background-image:none;background-position:0 0;background-repeat:unset;background-size:initial;padding-right:.75rem;-webkit-print-color-adjust:unset;print-color-adjust:unset}[type=checkbox],[type=radio]{-webkit-appearance:none;-moz-appearance:none;appearance:none;padding:0;-webkit-print-color-adjust:exact;print-color-adjust:exact;display:inline-block;vertical-align:middle;background-origin:border-box;-webkit-user-select:none;-moz-user-select:none;user-select:none;flex-shrink:0;height:1rem;width:1rem;color:#2563eb;background-color:#fff;border-color:rgba(var(--gray-500),var(--tw-border-opacity,1));border-width:1px;--tw-shadow:0 0 #0000}[type=checkbox]{border-radius:0}[type=radio]{border-radius:100%}[type=checkbox]:focus,[type=radio]:focus{outline:2px solid #0000;outline-offset:2px;--tw-ring-inset:var(--tw-empty,/*!*/ /*!*/);--tw-ring-offset-width:2px;--tw-ring-offset-color:#fff;--tw-ring-color:#2563eb;--tw-ring-offset-shadow:var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);--tw-ring-shadow:var(--tw-ring-inset) 0 0 0 calc(2px + var(--tw-ring-offset-width)) var(--tw-ring-color);box-shadow:var(--tw-ring-offset-shadow),var(--tw-ring-shadow),var(--tw-shadow)}[type=checkbox]:checked,[type=radio]:checked{border-color:#0000;background-color:currentColor;background-size:100% 100%;background-position:50%;background-repeat:no-repeat}[type=checkbox]:checked{background-image:url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='%23fff' viewBox='0 0 16 16'%3E%3Cpath d='M12.207 4.793a1 1 0 0 1 0 1.414l-5 5a1 1 0 0 1-1.414 0l-2-2a1 1 0 0 1 1.414-1.414L6.5 9.086l4.293-4.293a1 1 0 0 1 1.414 0z'/%3E%3C/svg%3E")}[type=radio]:checked{background-image:url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='%23fff' viewBox='0 0 16 16'%3E%3Ccircle cx='8' cy='8' r='3'/%3E%3C/svg%3E")}[type=checkbox]:checked:focus,[type=checkbox]:checked:hover,[type=checkbox]:indeterminate,[type=radio]:checked:focus,[type=radio]:checked:hover{border-color:#0000;background-color:currentColor}[type=checkbox]:indeterminate{background-image:url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 16 16'%3E%3Cpath stroke='%23fff' stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='M4 8h8'/%3E%3C/svg%3E");background-size:100% 100%;background-position:50%;background-repeat:no-repeat}[type=checkbox]:indeterminate:focus,[type=checkbox]:indeterminate:hover{border-color:#0000;background-color:currentColor}[type=file]{background:unset;border-color:inherit;border-width:0;border-radius:0;padding:0;font-size:unset;line-height:inherit}[type=file]:focus{outline:1px solid ButtonText;outline:1px auto -webkit-focus-ring-color}*,::backdrop,:after,:before{--tw-border-spacing-x:0;--tw-border-spacing-y:0;--tw-translate-x:0;--tw-translate-y:0;--tw-rotate:0;--tw-skew-x:0;--tw-skew-y:0;--tw-scale-x:1;--tw-scale-y:1;--tw-pan-x: ;--tw-pan-y: ;--tw-pinch-zoom: ;--tw-scroll-snap-strictness:proximity;--tw-gradient-from-position: ;--tw-gradient-via-position: ;--tw-gradient-to-position: ;--tw-ordinal: ;--tw-slashed-zero: ;--tw-numeric-figure: ;--tw-numeric-spacing: ;--tw-numeric-fraction: ;--tw-ring-inset: ;--tw-ring-offset-width:0px;--tw-ring-offset-color:#fff;--tw-ring-color:#3b82f680;--tw-ring-offset-shadow:0 0 #0000;--tw-ring-shadow:0 0 #0000;--tw-shadow:0 0 #0000;--tw-shadow-colored:0 0 #0000;--tw-blur: ;--tw-brightness: ;--tw-contrast: ;--tw-grayscale: ;--tw-hue-rotate: ;--tw-invert: ;--tw-saturate: ;--tw-sepia: ;--tw-drop-shadow: ;--tw-backdrop-blur: ;--tw-backdrop-brightness: ;--tw-backdrop-contrast: ;--tw-backdrop-grayscale: ;--tw-backdrop-hue-rotate: ;--tw-backdrop-invert: ;--tw-backdrop-opacity: ;--tw-backdrop-saturate: ;--tw-backdrop-sepia: }.hover\:bg-gray-950\/5:hover{background-color:rgba(var(--gray-950),.05)}.hover\:bg-transparent:hover{background-color:initial}.hover\:ring-gray-300:hover{--tw-ring-opacity:1;--tw-ring-color:rgba(var(--gray-300),var(--tw-ring-opacity))}.focus\:bg-gray-950\/5:focus{background-color:rgba(var(--gray-950),.05)}.group:hover .group-hover\:border{border-width:1px}.group:hover .group-hover\:border-primary-500\/10{border-color:rgba(var(--primary-500),.1)}.group:hover .group-hover\:bg-white{--tw-bg-opacity:1;background-color:rgb(255 255 255/var(--tw-bg-opacity))}.group:hover .group-hover\:text-primary-600{--tw-text-opacity:1;color:rgba(var(--primary-600),var(--tw-text-opacity))}.group:focus .group-focus\:text-white{--tw-text-opacity:1;color:rgb(255 255 255/var(--tw-text-opacity))}:is([dir=rtl] .rtl\:space-x-reverse)>:not([hidden])~:not([hidden]){--tw-space-x-reverse:1}:is(.dark .dark\:bg-gray-950){--tw-bg-opacity:1;background-color:rgba(var(--gray-950),var(--tw-bg-opacity))}:is(.dark .dark\:text-gray-200){--tw-text-opacity:1;color:rgba(var(--gray-200),var(--tw-text-opacity))}:is(.dark .dark\:ring-gray-500){--tw-ring-opacity:1;--tw-ring-color:rgba(var(--gray-500),var(--tw-ring-opacity))}:is(.dark .dark\:hover\:bg-white\/5:hover){background-color:#ffffff0d}:is(.dark .hover\:dark\:ring-gray-400):hover{--tw-ring-opacity:1;--tw-ring-color:rgba(var(--gray-400),var(--tw-ring-opacity))}:is(.dark .dark\:focus\:bg-white\/5:focus){background-color:#ffffff0d} \ No newline at end of file diff --git a/resources/views/components/flag.blade.php b/resources/views/components/flag.blade.php index 7325790..519fea8 100644 --- a/resources/views/components/flag.blade.php +++ b/resources/views/components/flag.blade.php @@ -8,14 +8,10 @@ src="{{ $src }}" {{ $attributes ->class([ - 'object-cover object-center max-w-none', + 'object-cover object-center', 'rounded-full' => $circular, 'rounded-lg' => ! $circular && ! $switch, 'rounded-md' => ! $circular && $switch, - '' ]) }} - @class([ - - ]) /> \ No newline at end of file diff --git a/resources/views/language-switch.blade.php b/resources/views/language-switch.blade.php index 1dffed0..e04e108 100644 --- a/resources/views/language-switch.blade.php +++ b/resources/views/language-switch.blade.php @@ -16,7 +16,7 @@
@if ($isVisibleOutsidePanels)
str_contains($outsidePanelsPlacement, 'top'), 'bottom-0' => str_contains($outsidePanelsPlacement, 'bottom'), 'justify-start' => str_contains($outsidePanelsPlacement, 'left'), diff --git a/resources/views/switch.blade.php b/resources/views/switch.blade.php index 8e46a12..3190871 100644 --- a/resources/views/switch.blade.php +++ b/resources/views/switch.blade.php @@ -65,7 +65,7 @@ class="w-7 h-7" :src="$languageSwitch->getFlag($locale)" :circular="$isCircular" :alt="$languageSwitch->getLabel($locale)" - class="p-0.5 w-7 h-7" + class="w-7 h-7" /> @else setLocale( - LanguageSwitch::make()->getPreferredLocale() + locale: LanguageSwitch::make()->getPreferredLocale() ); return $next($request); diff --git a/src/LanguageSwitch.php b/src/LanguageSwitch.php index e1c2dd9..bcef3e9 100755 --- a/src/LanguageSwitch.php +++ b/src/LanguageSwitch.php @@ -31,6 +31,8 @@ class LanguageSwitch extends Component protected array | Closure $locales = []; + protected bool $nativeLabel = false; + protected ?Placement $outsidePanelPlacement = null; protected bool | Closure $visibleInsidePanels = false; @@ -69,7 +71,7 @@ public static function boot(): void if ($static->isVisibleOutsidePanels()) { FilamentView::registerRenderHook( - name: 'panels::body.end', + name: 'panels::body.start', hook: fn (): string => Blade::render('') ); } @@ -82,14 +84,21 @@ public function circular(bool $condition = true): static return $this; } - public function displayLocale(?string $locale = null): static + public function displayLocale(string $locale = null): static { $this->displayLocale = $locale ?? app()->getLocale(); return $this; } - public function outsidePanelRoutes(array | Closure | null $routes = null): static + public function nativeLabel(bool $condition = true): static + { + $this->nativeLabel = $condition; + + return $this; + } + + public function outsidePanelRoutes(array | Closure $routes = null): static { $this->outsidePanelRoutes = $routes ?? [ 'auth.login', @@ -165,9 +174,9 @@ public function visible(bool | Closure $insidePanels = true, bool | Closure $out return $this; } - public function getDisplayLocale(): string + public function getDisplayLocale(): ?string { - return (string) $this->evaluate($this->displayLocale); + return $this->evaluate($this->displayLocale); } public function getExcludes(): array @@ -229,6 +238,11 @@ public function getLocales(): array return (array) $this->evaluate($this->locales); } + public function getNativeLabel(): bool + { + return (bool) $this->evaluate($this->nativeLabel); + } + public function getOutsidePanelPlacement(): Placement { return $this->outsidePanelPlacement ?? Placement::TopRight; @@ -278,7 +292,16 @@ public function getFlag(string $locale): string public function getLabel(string $locale): string { - return $this->labels[$locale] ?? str(locale_get_display_name($locale, $this->getDisplayLocale())) + if (array_key_exists($locale, $this->labels) && ! $this->getNativeLabel()) { + return strval($this->labels[$locale]); + } + + return str( + locale_get_display_name( + locale: $locale, + displayLocale: $this->getNativeLabel() ? $locale : $this->getDisplayLocale() + ) + ) ->title() ->toString(); } From 1d657b3f02419e4a5f6b1d92d73d3f8618e6cf7d Mon Sep 17 00:00:00 2001 From: bezhanSalleh Date: Mon, 1 Apr 2024 18:10:46 +0000 Subject: [PATCH 6/7] Fix styling --- src/LanguageSwitch.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/LanguageSwitch.php b/src/LanguageSwitch.php index bcef3e9..46d2785 100755 --- a/src/LanguageSwitch.php +++ b/src/LanguageSwitch.php @@ -84,7 +84,7 @@ public function circular(bool $condition = true): static return $this; } - public function displayLocale(string $locale = null): static + public function displayLocale(?string $locale = null): static { $this->displayLocale = $locale ?? app()->getLocale(); @@ -98,7 +98,7 @@ public function nativeLabel(bool $condition = true): static return $this; } - public function outsidePanelRoutes(array | Closure $routes = null): static + public function outsidePanelRoutes(array | Closure | null $routes = null): static { $this->outsidePanelRoutes = $routes ?? [ 'auth.login', From 08241ccfa4011363c65d05dc38df24e8ba3729a8 Mon Sep 17 00:00:00 2001 From: Bezhan Salleh Date: Mon, 1 Apr 2024 22:54:26 +0430 Subject: [PATCH 7/7] wip final --- src/LanguageSwitch.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/LanguageSwitch.php b/src/LanguageSwitch.php index bcef3e9..d8eb001 100755 --- a/src/LanguageSwitch.php +++ b/src/LanguageSwitch.php @@ -318,7 +318,7 @@ public function getCharAvatar(string $locale): string : str($locale)->upper()->toString(); } - public static function trigger($locale) + public static function trigger(string $locale) { session()->put('locale', $locale);