Skip to content

Commit

Permalink
Merge branch 'fix-language-switcher-routes' into add-option-to-dont-s…
Browse files Browse the repository at this point in the history
…etup-routes

# Conflicts:
#	resources/views/language-switcher.blade.php
#	routes/language-switcher.php
  • Loading branch information
pxpm committed Jan 17, 2024
2 parents 9c1a3fc + fd13326 commit c8a4769
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
9 changes: 8 additions & 1 deletion resources/views/language-switcher.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,16 @@
@endif
</a>
<ul class="dropdown-menu dropdown-menu-right dropdown-menu-end" style="right: 0">
@php
$useAdminPrefix = config('backpack.language-switcher.use_backpack_route_prefix_on_admin_routes');
@endphp
@foreach(config('backpack.crud.locales', []) as $locale => $name)
<li>
<a class="dropdown-item {{ $locale === $helper->getCurrentLocale() ? 'active disabled' : '' }}" href="{{ route('language-switcher.locale', ['locale' => $locale, 'backpack_prefix' => config('backpack.language-switcher.use_backpack_route_prefix_on_admin_routes') ? config('backpack.base.route_prefix') : null]) }}">
<a class="dropdown-item {{ $locale === $helper->getCurrentLocale() ? 'active disabled' : '' }}" href="{{ route('language-switcher.locale', [
'locale' => $useAdminPrefix ? $locale : null,
'backpack_prefix' => $useAdminPrefix ? config('backpack.base.route_prefix') : 'set-locale',
'setLocale' => $useAdminPrefix ? 'set-locale' : $locale
])}}">
@if($flags ?? true)
<span class="nav-link-icon" style="width: fit-content">
<x-dynamic-component component="flag-{{ $helper->getFlagOrFallback($locale) }}" style="width: 1.5rem" />
Expand Down
6 changes: 4 additions & 2 deletions routes/language-switcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
'middleware' => ['web', 'throttle:60,1'],
], function () {
// set locale
Route::any('{backpack_prefix?}/set-locale/{locale}', [\Backpack\LanguageSwitcher\Http\Controllers\LanguageSwitcherController::class, 'setLocale'])
->name('language-switcher.locale')->where('backpack_prefix', config('backpack.base.route_prefix'));
Route::any('{backpack_prefix?}/{setLocale}/{locale?}', [\Backpack\LanguageSwitcher\Http\Controllers\LanguageSwitcherController::class, 'setLocale'])
->name('language-switcher.locale')
->whereIn('setLocale', array_merge(['set-locale'],array_keys(config('backpack.crud.locales'))))
->whereIn('backpack_prefix', ['set-locale', config('backpack.base.route_prefix')]);
});
}

6 changes: 4 additions & 2 deletions src/Http/Controllers/LanguageSwitcherController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ class LanguageSwitcherController extends Controller
/**
* Set's the app locale
*/
public function setLocale(string $locale): Redirector | RedirectResponse
public function setLocale(?string $backpackPrefix = null, ?string $setLocale = null, ?string $locale = null): Redirector | RedirectResponse
{
$locale ??= $setLocale;

if (in_array($locale, array_keys(config('backpack.crud.locales')))) {
Session::put('backpack.language-switcher.locale', $locale);
}

return redirect(url()->previous());
return redirect()->back();
}
}

0 comments on commit c8a4769

Please sign in to comment.