Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
bezhanSalleh committed Jan 25, 2024
1 parent a6f7115 commit effed09
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 24 deletions.
27 changes: 3 additions & 24 deletions src/Http/Middleware/SwitchLanguageLocale.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
37 changes: 37 additions & 0 deletions src/LanguageSwitch.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<string, Panel>
*/
Expand Down

0 comments on commit effed09

Please sign in to comment.