Laravel Folio - how to manage localized URLs #49574
Unanswered
danielschweiger
asked this question in
General
Replies: 1 comment
-
I did the following (assuming you load the text from language files): // Register DE static pages from content/pages
Folio::path(base_path('resources/views/pages'))
->uri('de')
->middleware([
'*' => ['lang:de'],
]);
// Register EN static pages from content/pages
Folio::path(base_path('resources/views/pages'))
->uri('en')
->middleware([
'*' => ['lang:en'],
]);
// Register Preferred Language Redirect
Route::get('{path}', RedirectToPreferredLanguage::class)
->where('path', '^(?!(en|de)).*'); The middleware <?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Cookie;
class SetLanguage
{
/**
* 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, string $lang)
{
App::setLocale($lang);
// Remember the last known locale for automatic redirects
if ($request->cookie('last_known_locale', null) !== $lang) {
Cookie::queue('last_known_locale', $lang, 60 * 24 * 365);
}
return $next($request);
}
} And the redirect to preferred language I simply use the last known locale from the cookie or get the preferred language from the http headers: /**
* Determine the language from the request
*/
public static function determinePreferredLanguage(Request $request): string
{
$language = $request->cookie('last_known_locale', null);
if (! in_array($language, ['de', 'en'])) {
$language = null;
}
$language ??= $request->getPreferredLanguage(['en', 'de']);
return $language ?? config('app.fallback_locale');
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I am playing arround with Laravel Folio. In my URLs I need the localization (language code) as the first part:
domain.com/de/users
or
domain.com/en/users
Please check my attachment, how can I solve it without having "users" in both, "de" and "en" folders?
domain.com/users
is working great, but I need to have it here:domain.com/en/users
Beta Was this translation helpful? Give feedback.
All reactions