Skip to content

Commit

Permalink
Merge pull request #69 from bezhanSalleh/fix/browser-locales-issue
Browse files Browse the repository at this point in the history
fixes browser locale issue
  • Loading branch information
bezhanSalleh authored Jan 9, 2024
2 parents b6defe3 + 993e4d3 commit 2fded6e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 18 deletions.
16 changes: 3 additions & 13 deletions src/Events/LocaleChanged.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,10 @@

namespace BezhanSalleh\FilamentLanguageSwitch\Events;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class LocaleChanged
{
use Dispatchable;
use InteractsWithSockets;
use SerializesModels;

/**
* Create a new event instance.
*/
public function __construct(public string $locale)
{
public function __construct(
public string $locale
) {
}
}
14 changes: 9 additions & 5 deletions src/Http/Middleware/SwitchLanguageLocale.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@ public function handle(Request $request, Closure $next): \Illuminate\Http\Respon

private function getBrowserLocale(Request $request): ?string
{
$userLangs = preg_split('/[,;]/', $request->server('HTTP_ACCEPT_LANGUAGE'));
$appLocales = LanguageSwitch::make()->getLocales();

foreach ($userLangs as $locale) {
return in_array($locale, LanguageSwitch::make()->getLocales())
? $locale
: null;
$userLocales = preg_split('/[,;]/', $request->server('HTTP_ACCEPT_LANGUAGE'));

foreach ($userLocales as $locale) {
if (in_array($locale, $appLocales)) {
return $locale;
}
}

return null;
}
}

0 comments on commit 2fded6e

Please sign in to comment.