@@ -16,24 +16,23 @@ Simple and lightweight package to localize your Laravel routes.
16
16
- Redirects ` /fallback_locale/... ` to ` /... ` with 301 if needed
17
17
- Works with Laravel 10, 11, and 12
18
18
19
- ## Installation
19
+ ## 📚 Installation
20
20
21
21
Install the package via Composer:
22
22
23
23
``` bash
24
24
composer require alexwaha/laravel-multilang-routes
25
25
```
26
26
27
- Publish package config file:
27
+ Publish the configuration file:
28
+
28
29
``` bash
29
- php artisan vendor:publish --tag=multilang-localize -config
30
+ php artisan vendor:publish --tag=multilang-routes -config
30
31
```
31
32
32
- This will publish the config file to ` config/multilang-routes.php ` :
33
-
34
- You can customize the list of supported languages and their URL slugs.
33
+ This will create ` config/multilang-routes.php ` .
35
34
36
- ## Usage
35
+ ## 🚀 Usage
37
36
38
37
Define your routes using the ` localizedRoutes ` macro.
39
38
@@ -65,22 +64,24 @@ Route::localizedRoutes(function () {
65
64
}, ['web']);
66
65
```
67
66
68
- ### Checking if Route is Already Localized
69
- You can easily check if your route name is already localized before generating or using it.
70
- ``` php
71
- @if (Route::isLocalized($name))
72
- // It returns TRUE if The route name is already localized (e.g., "en.blog.index")
73
- @endif
74
- ```
75
-
76
67
This automatically generates localized routes like:
77
68
78
- - ` /en/about `
79
- - ` /es/about `
80
- - ` /de/about `
69
+ - ` /en/blog `
70
+ - ` /es/blog `
71
+ - ` /de/blog `
81
72
82
73
> The slug in the URL is required for the ` setLocale ` middleware, which changes the application language based on the ` locale ` slug.
83
74
75
+
76
+ ## 🧩 Checking if Route is Localized
77
+ You can easily check if your route name is already localized:
78
+
79
+ ``` php
80
+ @if (Route::isLocalized($name))
81
+ // The route name is already localized (e.g., "en.blog.index")
82
+ @endif
83
+ ```
84
+
84
85
### Blade usage
85
86
86
87
Generate a localized URL in Blade templates using ` Route::localize() ` method:
@@ -95,7 +96,8 @@ Or generate URLs with parameters:
95
96
<a href =" {{ Route::localize('blog.show', ['post' => $post]) }}" >View Post</a >
96
97
```
97
98
98
- ### Middleware: ` SetLocaleMiddleware `
99
+ ## 🛡️ Middleware
100
+ ### 🧩 ` localize.setLocale `
99
101
Middleware to automatically detect and set the application locale based on the first segment of the URL.
100
102
101
103
Registered alias as ` localize.setLocale `
@@ -140,13 +142,14 @@ Route::localizedRoutes(function () {
140
142
```
141
143
This is a simpler and more compact approach, especially useful for smaller projects.
142
144
143
- ### Middleware: ` PaginatedMiddleware `
145
+ ### 🧩 ` PaginatedMiddleware `
144
146
Middleware that transforms paginated URLs by cleaning query parameters.
145
147
146
148
Registered alias as ` localize.paginated ` .
147
149
148
150
When a paginated route like ` /page/{page?} ` is used:
149
151
152
+ 🛠 ** Note** :
150
153
* It ensures that the first page ` blog/page/1 ` does not have a query string or URL segment like ` /page/1 ` .
151
154
* Instead, the first page URL is clean, such as ` /blog ` , improving SEO and URL readability.
152
155
* If the user accesses ` blog/page/2 ` , ` blog/page/3 ` , etc., the page number remains in the URL.
@@ -172,7 +175,7 @@ If a user visits a URL with the default fallback locale (e.g., `/en/about` when
172
175
This ensures clean URLs for your default language.
173
176
174
177
175
- ### Configuration
178
+ ## ⚙️ Configuration
176
179
177
180
You can configure which languages are available and their corresponding URL slugs inside the ` config/multilang-routes.php ` file.
178
181
@@ -225,43 +228,26 @@ interface LanguageProviderInterface
225
228
```
226
229
This gives you the flexibility to load languages from the database, API, or any other source.
227
230
228
- Example of ` app/Providers/DatabaseLanguageProvider.php `
231
+ ---
229
232
230
- ``` php
231
- <?php
233
+ ## 📂 Examples
232
234
233
- namespace App\Providers;
235
+ You can find practical examples inside the [ examples ] ( ./examples ) folder:
234
236
235
- use App\Contracts\LanguageProviderInterface;
236
- use App\Contracts\LanguageInterface;
237
- use App\Models\Language;
237
+ - [ ExampleController.php] ( examples/ExampleController.php )
238
+ Simple controller with paginated navigation.
239
+
240
+ - [ DatabaseLanguageProvider.php] ( examples/DatabaseLanguageProvider.php )
241
+ Custom Language Provider loading languages from database.
242
+
243
+ - [ RoutesExample.php] ( examples/RoutesExample.php )
244
+ Localized routes registration example.
245
+
246
+ - [ pagination.blade.php] ( examples/pagination.blade.php )
247
+ Blade pagination rendering.
248
+
249
+ ---
238
250
239
- class DatabaseLanguageProvider implements LanguageProviderInterface
240
- {
241
- /**
242
- * @return array<LanguageInterface >
243
- */
244
- public function getLanguages(): array
245
- {
246
- $languages = Language::all();
247
-
248
- return array_map(function (Language $language) {
249
- return new \Alexwaha\Localize\Language(
250
- $language->locale,
251
- $language->slug,
252
- $language->is_default
253
- );
254
- }, $languages->all());
255
- }
256
-
257
- public function getLocaleBySegment(?string $segment = null): string
258
- {
259
- $language = Language::where('slug', $segment)->first();
260
-
261
- return $language?->locale ?? config('app.fallback_locale');
262
- }
263
- }
264
- ```
265
251
266
252
## Requirements
267
253
0 commit comments