Laravel Multi-language routes without prefix



PHP Snippet 1:

return [
    'post' => '/post/',
    'product' => '/product/',
    'contact' => '/contact/',
    'aboutUs' => '/about-us/'
];

PHP Snippet 2:

enum LanguageType: string
{
    case EN = 'en';
    case DE = 'de';

    public function label(): string
    {
        return trans(match ($this) {
            self::EN => 'English',
            self::DE => 'German',
        });
    }
}

PHP Snippet 3:

Route::get('{contactTranslation}', [ContactController::class, 'index']);
Route::get('{aboutUsTranslation}', [AboutUsController::class, 'index']);
Route::get('{productTranslation}', [ProductController::class, 'index']);
Route::get('{postTranslation}', [PostController::class, 'index']);
//...

PHP Snippet 4:

foreach (trans('routes') as $key => $value) {
    Route::pattern($key . 'Translation', $this->getTranslation($key));
}

PHP Snippet 5:

private function getTranslation($slug): string
{
    $slugList = collect();
    foreach (LanguageType::cases() as $language) {
        $slugList = $slugList->merge(trim(trans('routes.' . $slug, [], $language->value), '/'));
    }

    return $slugList->implode('|');
}