alexwaha/laravel-multilang-routes
Composer 安装命令:
composer require alexwaha/laravel-multilang-routes
包简介
Multilanguage Localization for Laravel routes.
README 文档
README
Simple and lightweight package to localize your Laravel routes.
Features
- Localized versions of your routes without complex configurations
- Seamless support for route groups, slugs, and middleware
- Automatically switches URLs based on the active locale
- Redirects
/fallback_locale/...to/...with 301 if needed - Works with Laravel 10, 11, and 12
📚 Installation
Install the package via Composer:
composer require alexwaha/laravel-multilang-routes
Publish the configuration file:
php artisan vendor:publish --tag=multilang-routes-config
This will create config/multilang-routes.php.
🚀 Usage
Define your routes using the localizedRoutes macro.
use Illuminate\Support\Facades\Route; Route::localizedRoutes(function () { Route::get('/', function () { return view('welcome'); }); });
Or routes with middlewares, for ex.: web
use Illuminate\Support\Facades\Route; Route::localizedRoutes(function () { Route::get('/', [HomeController::class, 'index'])->name('home'); }, ['web']);
Or named routes with prefix
Route::localizedRoutes(function () { Route::name('blog.')->prefix('/blog')->group(function () { Route::get('', [BlogController::class, 'index'])->name('index'); Route::get('/{post}', [BlogController::class, 'show'])->name('show'); }); }, ['web']);
This automatically generates localized routes like:
/en/blog/es/blog/de/blog
The slug in the URL is required for the
setLocalemiddleware, which changes the application language based on thelocaleslug.
🧩 Checking if Route is Localized
You can easily check if your route name is already localized:
@if (Route::isLocalized($name)) // The route name is already localized (e.g., "en.blog.index") @endif
Blade usage
Generate a localized URL in Blade templates using Route::localize() method:
<a href="{{ Route::localize('about') }}">About</a>
Or generate URLs with parameters:
<a href="{{ Route::localize('blog.show', ['post' => $post]) }}">View Post</a>
🛡️ Middleware
🧩 localize.setLocale
Middleware to automatically detect and set the application locale based on the first segment of the URL.
Registered alias as localize.setLocale
Note: If the segment does not match any configured language slug, the application will fall back to the default locale.
➊ Apply Middleware Globally to a middleware group
in Laravel 10.x app\Http\Kernel.php
protected $middlewareGroups = [ 'web' => [ \Alexwaha\Localize\Middleware\SetLocale::class,
in Laravel ^11.x bootstrap\app.php
->withMiddleware(function (Middleware $middleware) { $middleware->group('web', [ \Alexwaha\Localize\Middleware\SetLocale::class,, ]); })
Route::localizedRoutes(function () { Route::get('/', function () { return view('welcome'); }); }, ['web']);
➋ Apply Middleware Directly in localizedRoutes
You can pass middleware as the second argument to the localizedRoutes macro:
Route::localizedRoutes(function () { Route::get('/', function () { return view('welcome'); }); }, ['web', 'localize.setLocale']);
This is a simpler and more compact approach, especially useful for smaller projects.
🧩 PaginatedMiddleware
Middleware that transforms paginated URLs by cleaning query parameters.
Registered alias as localize.paginated.
When a paginated route like /page/{page?} is used:
🛠 Note:
- It ensures that the first page
blog/page/1does not have a query string or URL segment like/page/1. - Instead, the first page URL is clean, such as
/blog, improving SEO and URL readability. - If the user accesses
blog/page/2,blog/page/3, etc., the page number remains in the URL.
When building localized URLs with pagination, you can define your routes like this:
use Illuminate\Support\Facades\Route; Route::localizedRoutes(function () { Route::name('blog.')->prefix('/blog')->group(function () { Route::get('', [BlogController::class, 'index'])->name('index'); Route::get('/page/{page?}', [BlogController::class, 'index'])->name('paginated'); Route::get('/{post}', [BlogController::class, 'show'])->name('show'); }); }, ['web', 'localize.setLocale', 'localize.paginated']);
Default Locale Redirection
If a user visits a URL with the default fallback locale (e.g., /en/about when en is the fallback), they will be automatically redirected with a 301 Permanent Redirect to the non-sluged version (/about).
This ensures clean URLs for your default language.
⚙️ Configuration
You can configure which languages are available and their corresponding URL slugs inside the config/multilang-routes.php file.
Each language entry should contain:
| Key | Description | Example |
|---|---|---|
| locale | Application locale (app()->setLocale) |
en,es,de |
| slug | URL slug for routes | en,es,de | english, spanish, german |
Example:
return [ 'language_provider' => [ 'class' => \Alexwaha\Localize\LanguageProvider::class, 'params' => [ 'languages' => [ [ 'locale' => 'en', 'slug' => 'en', ], [ 'locale' => 'es', 'slug' => 'es', ], ], ], ], ];
You can customize the list of supported languages and their URL prefixes or create your own Language Provider.
If you want full control over how languages are loaded (for example, from a database), you can create a custom provider by implementing the LanguageProviderInterface and specify your class in the configuration.
Example config/multilang-routes.php:
return [ 'language_provider' => [ 'class' => App\Providers\CustomLanguageProvider::class, ], ];
Your custom provider should implement:
interface LanguageProviderInterface { public function getLanguages(): array; public function getLocaleBySegment(?string $segment = null): string; }
This gives you the flexibility to load languages from the database, API, or any other source.
📂 Examples
You can find practical examples inside the examples folder:
-
ExampleController.php
Simple controller with paginated navigation. -
DatabaseLanguageProvider.php
Custom Language Provider loading languages from database. -
RoutesExample.php
Localized routes registration example. -
pagination.blade.php
Blade pagination rendering.
Requirements
- PHP >= 8.2
- Laravel 10.x, 11.x, or 12.x
License
This package is open-sourced software licensed under the MIT license.
Credits
Developed by Alex Waha
Feel free to submit issues or contribute to this project!
alexwaha/laravel-multilang-routes 适用场景与选型建议
alexwaha/laravel-multilang-routes 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 20 次下载、GitHub Stars 达 7, 最近一次更新时间为 2025 年 04 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「routes」 「laravel」 「localization」 「multilingual」 「localize」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 alexwaha/laravel-multilang-routes 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 alexwaha/laravel-multilang-routes 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 alexwaha/laravel-multilang-routes 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Generates a Blade directive exporting all of your named Laravel routes. Also provides a nice route() helper function in JavaScript.
Block routes by IP
Package for convenient work with Laravel's localization features
Field for number with restricted count of digits and decimal places
Provides caching methods which can be easily used for caching routes.
Deepl Automated translations for Laravel lang files
统计信息
- 总下载量: 20
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 29
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-04-10