PHPackages                             larasofthu/localized-routes-plus - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Localization &amp; i18n](/categories/localization)
4. /
5. larasofthu/localized-routes-plus

ActiveLibrary[Localization &amp; i18n](/categories/localization)

larasofthu/localized-routes-plus
================================

Advanced Laravel package for creating localized routes with subdomain support, country-specific routing, and automatic locale management. Supports URL prefixes, subdomains, and multi-country localization strategies.

1.0.7(9mo ago)139MITPHPPHP ^8.0CI passing

Since Jun 30Pushed 9mo agoCompare

[ Source](https://github.com/LarasoftHU/localized-routes-plus)[ Packagist](https://packagist.org/packages/larasofthu/localized-routes-plus)[ Docs](https://github.com/LarasoftHU/localized-routes-plus)[ RSS](/packages/larasofthu-localized-routes-plus/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (13)Versions (29)Used By (0)

Laravel Localized Routes Plus
=============================

[](#laravel-localized-routes-plus)

[![Latest Version on Packagist](https://camo.githubusercontent.com/28a0929ee5e8fdc8e08c339788e5bd59d216b6f72de9d4d360845fb262e2453d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f4c617261736f667448552f6c6f63616c697a65642d726f757465732d706c75732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/LarasoftHU/localized-routes-plus)[![GitHub Tests Action Status](https://camo.githubusercontent.com/b72433faf9bc1a529016ab67b164c569ba02f81311ff2d128bc5f45ac6320a4d/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f4c617261736f667448552f6c6f63616c697a65642d726f757465732d706c75732f74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/LarasoftHU/localized-routes-plus/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/f80c29813aa709ad745083d61f55bcd2a9b08f71a4f22c8932311c849a6e1098/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f4c617261736f667448552f6c6f63616c697a65642d726f757465732d706c75732f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/LarasoftHU/localized-routes-plus/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/dd1803ab4039e3716275f1438f99648e16035c2bcf4a81d86fd68ee2f8ec473e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f4c617261736f667448552f6c6f63616c697a65642d726f757465732d706c75732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/LarasoftHU/localized-routes-plus)

A powerful Laravel package for creating localized routes with advanced features including subdomain support, country-specific routing, and automatic locale management.

Features
--------

[](#features)

🌍 **Multiple Localization Strategies**

- URL prefix-based localization (`/en/page`, `/hu/page`)
- Subdomain-based localization (`en.example.com`, `hu.example.com`)
- Country-specific routing (`/en-us/page`, `/en-ca/page`)

🚀 **Framework Integration**

- Seamless Laravel Route integration
- Automatic middleware registration
- Resource route support
- Custom route model binding

⚙️ **Flexible Configuration**

- Whitelist/blacklist locales
- Configurable separators
- Multiple domains per locale
- Default locale customization

🔧 **Developer-Friendly**

- Intuitive API design
- Helper methods for URL generation
- Comprehensive testing
- Full IDE support

Installation
------------

[](#installation)

Install the package via Composer:

```
composer require larasofthu/localized-routes-plus
```

Publish the configuration file:

```
php artisan vendor:publish --tag="localized-routes-plus-config"
```

Quick Start
-----------

[](#quick-start)

### Basic Usage

[](#basic-usage)

Create localized routes by chaining the `localized()` method:

```
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)
```

### Resource Routes

[](#resource-routes)

```
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.
```

### Helper Functions

[](#helper-functions)

The package provides convenient helper functions for common tasks:

```
// 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]);
```

```
{{-- Language switcher in Blade --}}

    @foreach(config('localized-routes-plus.locales') as $locale)
        {{ $locale }}
    @endforeach

```

Configuration
-------------

[](#configuration)

The configuration file (`config/localized-routes-plus.php`) contains all available options:

```
return [
    // Available locales
    'locales' => ['en', 'hu', 'de'],

    // Default locale
    'default_locale' => 'en',

    // Include prefix for default locale in URLs
    'use_route_prefix_in_default_locale' => false,

    // Subdomain configuration
    'use_subdomains_instead_of_prefixes' => false,
    'domains' => [
        'en' => 'example.com',
        'hu' => 'example.hu',
        'de' => 'de.example.com',
    ],

    // Country-specific routing
    'use_countries' => false,
    'country_path_separator' => 'dash', // 'dash' or 'slash'
    'countries' => [
        'en' => 'us',
        'hu' => 'hu',
        'de' => 'de',
    ],
];
```

URL Prefix Localization
-----------------------

[](#url-prefix-localization)

### Basic Configuration

[](#basic-configuration)

```
// config/localized-routes-plus.php
'locales' => ['en', 'hu', 'de'],
'default_locale' => 'en',
'use_route_prefix_in_default_locale' => false,
```

### Route Examples

[](#route-examples)

```
Route::get('products', [ProductController::class, 'index'])
    ->name('products.index')
    ->localized();

// Generated routes:
// GET /products -> en.products.index (default locale, no prefix)
// GET /hu/products -> hu.products.index
// GET /de/products -> de.products.index
```

### Including Default Locale in URLs

[](#including-default-locale-in-urls)

```
// config/localized-routes-plus.php
'use_route_prefix_in_default_locale' => true,

// Results in:
// GET /en/products -> en.products.index
// GET /hu/products -> hu.products.index
// GET /de/products -> de.products.index
```

Subdomain Localization
----------------------

[](#subdomain-localization)

### Configuration

[](#configuration-1)

```
// config/localized-routes-plus.php
'use_subdomains_instead_of_prefixes' => true,
'domains' => [
    'en' => 'example.com',
    'hu' => 'example.hu',
    'de' => 'de.example.com',
],
```

### Multiple Domains per Locale

[](#multiple-domains-per-locale)

```
'domains' => [
    'en' => 'example.com',
    'hu' => 'example.hu',
    'de' => [
        'de.example.com',
        'de2.example.com',
        'example.de'
    ],
],
```

### Route Examples

[](#route-examples-1)

```
Route::get('products', [ProductController::class, 'index'])
    ->name('products.index')
    ->localized();

// Generated routes:
// example.com/products -> en.products.index
// example.hu/products -> hu.products.index
// de.example.com/products -> de.products.index
// de2.example.com/products -> de.products.index (additional domain)
// example.de/products -> de.products.index (additional domain)
```

Country-Specific Routing
------------------------

[](#country-specific-routing)

### Configuration

[](#configuration-2)

```
// config/localized-routes-plus.php
'use_countries' => true,
'country_path_separator' => 'dash',
'countries' => [
    'en' => 'us',
    'hu' => 'hu',
    'de' => 'de',
],
```

### Multiple Countries per Locale

[](#multiple-countries-per-locale)

```
'countries' => [
    'en' => ['us', 'ca', 'gb'],
    'hu' => 'hu',
    'de' => 'de',
],
```

### Path Separators

[](#path-separators)

```
// Dash separator (default)
'country_path_separator' => 'dash',
// Results in: /en-us/products, /en-ca/products

// Slash separator
'country_path_separator' => 'slash',
// Results in: /en/us/products, /en/ca/products
```

### Route Examples

[](#route-examples-2)

```
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
```

Selective Localization
----------------------

[](#selective-localization)

### Whitelist Specific Locales

[](#whitelist-specific-locales)

```
// 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');
```

### Blacklist Specific Locales

[](#blacklist-specific-locales)

```
// 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']);
```

Advanced Usage
--------------

[](#advanced-usage)

### Route Model Binding

[](#route-model-binding)

```
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
```

### Route Parameters

[](#route-parameters)

```
Route::get('categories/{category}/products/{product}', [ProductController::class, 'show'])
    ->name('products.show')
    ->localized();

// Generated routes handle parameters correctly:
// /categories/electronics/products/laptop -> en.products.show
// /hu/categories/electronics/products/laptop -> hu.products.show
```

### Middleware Integration

[](#middleware-integration)

The package automatically registers middleware to set the application locale:

```
// Automatically applied to localized routes
SetLocaleFromRoute::class  // Sets App::setLocale()
SetCountryFromRoute::class // Sets App::setCountry() (when countries enabled)
```

Helper Methods
--------------

[](#helper-methods)

### Route Switching

[](#route-switching)

```
// 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');
```

### URL Generation

[](#url-generation)

```
// 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 Information

[](#route-information)

```
$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)
}
```

### Global Helper Functions

[](#global-helper-functions)

The package provides convenient global helper functions:

#### `current_route()`

[](#current_route)

Get the current route URL in a different 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);
```

#### `route_is()`

[](#route_is)

Check if the current route matches a name (locale-agnostic):

```
// 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
}

...
```

#### `localized_route()`

[](#localized_route)

Generate localized route URLs:

```
// 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);
```

Localized URI Translation
-------------------------

[](#localized-uri-translation)

The package supports translating URI segments using Laravel's language files. This allows you to have different URL structures for different locales while keeping the same route logic.

### Setup

[](#setup)

Create language files for your routes in the `lang/{locale}/routes.php` files:

```
// lang/en/routes.php
