PHP code example of larasofthu / localized-routes-plus
1. Go to this page and download the library: Download larasofthu/localized-routes-plus library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
larasofthu / localized-routes-plus example snippets
use Illuminate\Support\Facades\Route;
// Creates routes for all configured locales
Route::get('about', function () {
return view('about');
})->name('about')->localized();
// Results in:
// /about (default locale)
// /hu/about (Hungarian)
// /de/about (German)
Route::resource('posts', PostController::class)
->names('posts')
->localized();
// Creates localized versions of all resource routes:
// en.posts.index, en.posts.create, en.posts.store, etc.
// hu.posts.index, hu.posts.create, hu.posts.store, etc.
// Get current route in different locale
$germanUrl = current_route('de');
// Check route name (works for any locale)
if (route_is('about')) {
// Current page is about page
}
// Generate localized routes
$url = localized_route('products.show', ['product' => $product]);
Route::get('products', [ProductController::class, 'index'])
->name('products.index')
->localized();
// With dash separator:
// GET /en-us/products -> en-us.products.index
// GET /en-ca/products -> en-ca.products.index
// Only create routes for English and Hungarian
Route::get('admin', [AdminController::class, 'index'])
->name('admin.index')
->localized(['en', 'hu']);
// Single locale
Route::get('terms', function () {
return view('terms');
})->name('terms')->localized('en');
// Create routes for all locales except German
Route::get('news', [NewsController::class, 'index'])
->name('news.index')
->localizedExcept('de');
// Multiple locales
Route::get('blog', [BlogController::class, 'index'])
->name('blog.index')
->localizedExcept(['de', 'fr']);
Route::get('posts/{post}', [PostController::class, 'show'])
->name('posts.show')
->localized();
// Works with model binding:
// /posts/my-post-slug -> en.posts.show
// /hu/posts/my-post-slug -> hu.posts.show
// Automatically applied to localized routes
SetLocaleFromRoute::class // Sets App::setLocale()
SetCountryFromRoute::class // Sets App::setCountry() (when countries enabled)
// In your views or controllers
$currentRoute = request()->route();
// Get route for different locale
$germanRoute = $currentRoute->locale('de');
$germanUrl = $germanRoute->getUrl();
// With countries
$usRoute = $currentRoute->locale('en', 'us');
$canadaRoute = $currentRoute->locale('en', 'ca');
// Standard Laravel route() helper works
$url = route('products.index'); // Current locale
$hungarianUrl = route('hu.products.index'); // Specific locale
// With countries
$usUrl = route('en-us.products.index');
$canadaUrl = route('en-ca.products.index');
// Using helper methods
$route = request()->route();
$germanUrl = $route->getUrl('de');
$usUrl = $route->getUrl('en', 'us'); // With country
$route = request()->route();
// Get locale and country
$locale = $route->getLocale(); // 'en'
$country = $route->getCountry(); // 'us' (if countries enabled)
// Get route name without locale prefix
$safeName = $route->getSafeName(); // 'products.index'
// Check if route matches a name
if ($route->is('products.index')) {
// Current route is products.index (any locale)
}
// Get current route in German
$germanUrl = current_route('de');
// With countries - EN-US
$usUrl = current_route('en', 'us');
// With custom parameters
$url = current_route('hu', null, ['id' => 123]);
// Relative URL
$relativeUrl = current_route('de', null, [], false);
// Traditional Laravel way (locale-specific)
if (request()->route()->getName() === 'en.products.index') {
// Only matches English version
}
// Using route_is() helper (works for any locale)
if (route_is('products.index')) {
// Matches en.products.index, hu.products.index, de.products.index, etc.
// you can use it in balde templates see below
}
<nav class="...@if(route_is('products.index')selected@endif">...</nav>
// Generate route for current locale
$url = localized_route('products.show', ['product' => $product]);
// Equivalent to:
$locale = app()->getLocale();
$url = route($locale . '.products.show', ['product' => $product]);
// Automatic with countries (when enabled)
$url = localized_route('products.show', ['product' => $product]);
// Uses current locale and country automatically
// You can add custom locale and country if you want
$url = localized_route('products.show', ['product' => $product], $absolute, $locale, $country);
// Make sure all locales have corresponding domains when using subdomains
'domains' => [
'en' => 'example.com',
'hu' => 'example.hu', // Don't forget this!
],