{"id":26085283,"url":"https://github.com/codezero-be/laravel-localized-routes","last_synced_at":"2025-05-15T10:01:08.733Z","repository":{"id":31054276,"uuid":"126732801","full_name":"codezero-be/laravel-localized-routes","owner":"codezero-be","description":"⭐️ A convenient way to set up and use localized routes in a Laravel app.","archived":false,"fork":false,"pushed_at":"2024-03-17T22:44:54.000Z","size":407,"stargazers_count":532,"open_issues_count":16,"forks_count":63,"subscribers_count":15,"default_branch":"master","last_synced_at":"2025-04-20T15:02:01.423Z","etag":null,"topics":["country","language","laravel","locale","localization","php","routing","translations"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/codezero-be.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":".github/SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"ko_fi":"ivanvermeyen","custom":"https://paypal.me/ivanvermeyen"}},"created_at":"2018-03-25T19:33:39.000Z","updated_at":"2025-04-19T20:22:09.000Z","dependencies_parsed_at":"2024-03-10T16:29:38.901Z","dependency_job_id":"fee8950e-04ca-4575-bef8-7a291f76c73a","html_url":"https://github.com/codezero-be/laravel-localized-routes","commit_stats":{"total_commits":215,"total_committers":16,"mean_commits":13.4375,"dds":0.09302325581395354,"last_synced_commit":"0a1b6fc5f9b24517b7404839bc5f1eba155ef01c"},"previous_names":[],"tags_count":51,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codezero-be%2Flaravel-localized-routes","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codezero-be%2Flaravel-localized-routes/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codezero-be%2Flaravel-localized-routes/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codezero-be%2Flaravel-localized-routes/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/codezero-be","download_url":"https://codeload.github.com/codezero-be/laravel-localized-routes/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254319715,"owners_count":22051072,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["country","language","laravel","locale","localization","php","routing","translations"],"created_at":"2025-03-09T05:58:14.828Z","updated_at":"2025-05-15T10:01:07.986Z","avatar_url":"https://github.com/codezero-be.png","language":"PHP","funding_links":["https://ko-fi.com/ivanvermeyen","https://paypal.me/ivanvermeyen","https://ko-fi.com/R6R3UQ8V"],"categories":[],"sub_categories":[],"readme":"# Laravel Localized Routes\n\n[![GitHub release](https://img.shields.io/github/release/codezero-be/laravel-localized-routes.svg?style=flat-square)](https://github.com/codezero-be/laravel-localized-routes/releases)\n[![Laravel](https://img.shields.io/badge/laravel-11-red?style=flat-square\u0026logo=laravel\u0026logoColor=white)](https://laravel.com)\n[![License](https://img.shields.io/packagist/l/codezero/laravel-localized-routes.svg?style=flat-square)](LICENSE.md)\n[![Build Status](https://img.shields.io/github/actions/workflow/status/codezero-be/laravel-localized-routes/run-tests.yml?style=flat-square\u0026logo=github\u0026logoColor=white\u0026label=tests)](https://github.com/codezero-be/laravel-localized-routes/actions)\n[![Code Coverage](https://img.shields.io/codacy/coverage/a5db8a1321664e67900c96eadc575ece/master?style=flat-square)](https://app.codacy.com/gh/codezero-be/laravel-localized-routes)\n[![Code Quality](https://img.shields.io/codacy/grade/a5db8a1321664e67900c96eadc575ece/master?style=flat-square)](https://app.codacy.com/gh/codezero-be/laravel-localized-routes)\n[![Total Downloads](https://img.shields.io/packagist/dt/codezero/laravel-localized-routes.svg?style=flat-square)](https://packagist.org/packages/codezero/laravel-localized-routes)\n\n[![ko-fi](https://www.ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/R6R3UQ8V)\n\nA convenient way to set up and use localized routes in a Laravel app.\n\n## 📖 Table of Contents\n\n- [Requirements](#-requirements)\n- [Upgrade](#-upgrade)\n- [Install](#-install)\n- [Configure](#-configure)\n  - [Publish Configuration File](#-publish-configuration-file)\n  - [Configure Supported Locales](#-configure-supported-locales)\n    - [Simple Locales](#simple-locales)\n    - [Custom Slugs](#custom-slugs)\n    - [Custom Domains](#custom-domains)\n  - [Use Fallback Locale](#-use-fallback-locale)\n  - [Omit Slug for Main Locale](#-omit-slug-for-main-locale)\n  - [Scoped Options](#-scoped-options)\n- [Add Middleware to Update App Locale](#-add-middleware-to-update-app-locale)\n  - [Detectors](#detectors)\n  - [Stores](#stores)\n- [Register Routes](#-register-routes)\n  - [Translate Parameters with Route Model Binding](#-translate-parameters-with-route-model-binding)\n  - [Translate Hard-Coded URI Slugs](#-translate-hard-coded-uri-slugs)\n- [Localize 404 Pages](#-localize-404-pages)\n- [Cache Routes](#-cache-routes)\n- [Generate Route URLs](#-generate-route-urls)\n  - [Generate URLs for the Active Locale](#-generate-urls-for-the-active-locale)\n  - [Generate URLs for a Specific Locale](#-generate-urls-for-a-specific-locale)\n  - [Generate URLs with Localized Parameters](#-generate-urls-with-localized-parameters)\n  - [Fallback URLs](#-fallback-urls)\n  - [Generate Localized Versions of the Current URL](#-generate-localized-versions-of-the-current-url)\n  - [Example Locale Switcher](#-example-locale-switcher)\n- [Generate Signed Route URLs](#-generate-signed-route-urls)\n- [Redirect to Routes](#-redirect-to-routes)\n- [Automatically Redirect to Localized URLs](#-automatically-redirect-to-localized-urls)\n- [Helpers](#-helpers)\n- [Testing](#-testing)\n- [Credits](#-credits)\n- [Security](#-security)\n- [Changelog](#-changelog)\n- [License](#-license)\n\n## ✅ Requirements\n\n- PHP \u003e= 8.1\n- Laravel \u003e= 10\n- Composer ^2.3 (for [codezero/composer-preload-files](https://github.com/codezero-be/composer-preload-files))\n\n## ⬆ Upgrade\n\nUpgrading to a new major version?\nCheck our [upgrade guide](UPGRADE.md) for instructions.\n\n## 📦 Install\n\nInstall this package with Composer:\n\n```bash\ncomposer require codezero/laravel-localized-routes\n```\n\nLaravel will automatically register the ServiceProvider.\n\n## ⚙ Configure\n\n### ☑ Publish Configuration File\n\n```bash\nphp artisan vendor:publish --provider=\"CodeZero\\LocalizedRoutes\\LocalizedRoutesServiceProvider\" --tag=\"config\"\n```\n\nYou will now find a `localized-routes.php` file in the `config` folder.\n\n### ☑ Configure Supported Locales\n\n#### Simple Locales\n\nAdd any locales you wish to support to your published `config/localized-routes.php` file:\n\n```php\n'supported_locales' =\u003e ['en', 'nl'];\n```\n\nThese locales will be used as a slug, prepended to the URL of your localized routes.\n\n#### Custom Slugs\n\nYou can also use a custom slug for a locale:\n\n```php\n'supported_locales' =\u003e [\n    'en' =\u003e 'english-slug',\n    'nl' =\u003e 'dutch-slug',\n];\n```\n\n#### Custom Domains\n\nOr you can use a custom domain for a locale:\n\n```php\n'supported_locales' =\u003e [\n    'en' =\u003e 'english-domain.test',\n    'nl' =\u003e 'dutch-domain.test',\n];\n```\n\n### ☑ Use Fallback Locale\n\nWhen using the `route()` helper to generate a URL for a locale that is not supported, a `Symfony\\Component\\Routing\\Exception\\RouteNotFoundException` is thrown by Laravel.\nHowever, you can configure a fallback locale to attempt to resolve a fallback URL instead.\nIf that fails too, the exception is thrown.\n\n```php\n'fallback_locale' =\u003e 'en',\n```\n\n### ☑ Omit Slug for Main Locale\n\nSpecify your main locale if you want to omit its slug from the URL:\n\n```php\n'omitted_locale' =\u003e 'en',\n```\n\nThis option has no effect if you use domains instead of slugs.\n\n### ☑ Scoped Options\n\nTo set an option for one localized route group only, you can specify it as the second parameter of the localized route macro.\nThis will override the config file settings. Currently, only 2 options can be overridden.\n\n```php\nRoute::localized(function () {\n    Route::get('about', [AboutController::class, 'index']);\n}, [\n    'supported_locales' =\u003e ['en', 'nl', 'fr'],\n    'omitted_locale' =\u003e 'en',\n]);\n```\n\n## 🧩 Add Middleware to Update App Locale\n\nBy default, the app locale will always be what you configured in `config/app.php`.\nTo automatically update the app locale, you need to register the middleware in the `web` middleware group.\nMake sure to add it after `StartSession` and before `SubstituteBindings`.\n\nThe order of the middleware is important if you are using localized route keys (translated slugs)!\nThe session needs to be active when setting the locale, and the locale needs to be set when substituting the route bindings.\n\n### Laravel 11 and newer:\n\nAdd the middleware to the `web` middleware group in `bootstrap/app.php`.\n\n```php\n// bootstrap/app.php\n-\u003ewithMiddleware(function (Middleware $middleware) {\n    $middleware-\u003eweb(remove: [\n        \\Illuminate\\Routing\\Middleware\\SubstituteBindings::class,\n    ]);\n    $middleware-\u003eweb(append: [\n        \\CodeZero\\LocalizedRoutes\\Middleware\\SetLocale::class,\n        \\Illuminate\\Routing\\Middleware\\SubstituteBindings::class,\n    ]);\n})\n```\n\n### Laravel 10:\n\nAdd the middleware to the `web` middleware group in `app/Http/Kernel.php`.\n\n```php\n// app/Http/Kernel.php\nprotected $middlewareGroups = [\n    'web' =\u003e [\n        //...\n        \\Illuminate\\Session\\Middleware\\StartSession::class, // \u003c= after this\n        //...\n        \\CodeZero\\LocalizedRoutes\\Middleware\\SetLocale::class,\n        \\Illuminate\\Routing\\Middleware\\SubstituteBindings::class, // \u003c= before this\n    ],\n];\n```\n\n### Detectors\n\nThe middleware runs the following detectors in sequence, until one returns a supported locale:\n\n|  #  | Detector                | Description                                                            |\n|:---:|-------------------------|------------------------------------------------------------------------|\n| 1.  | `RouteActionDetector`   | Required. The locales of a localized route is saved in a route action. |\n| 2.  | `UrlDetector`           | Required. Tries to find a locale based on the URL slugs or domain.     |\n| 3.  | `OmittedLocaleDetector` | Required if an omitted locale is configured. This will always be used. |\n| 4.  | `UserDetector`          | Checks a configurable `locale` attribute on the authenticated user.    |\n| 5.  | `SessionDetector`       | Checks the session for a previously stored locale.                     |\n| 6.  | `CookieDetector`        | Checks a cookie for a previously stored locale.                        |\n| 7.  | `BrowserDetector`       | Checks the preferred language settings of the visitor's browser.       |\n| 8.  | `AppDetector`           | Required. Checks the default app locale as a last resort.              |\n\nUpdate the `detectors` array in the config file to choose which detectors to run and in what order.\n\n\u003e You can create your own detector by implementing the `CodeZero\\LocalizedRoutes\\Middleware\\Detectors\\Detector` interface and add a reference to it in the config file. The detectors are resolved from Laravel's IOC container, so you can add any dependencies to your constructor.\n\n### Stores\n\nIf a supported locale is detected, it will automatically be stored in:\n\n|  #  | Store          | Description                                         |\n|:---:|----------------|-----------------------------------------------------|\n| 1.  | `SessionStore` | Stores the locale in the session.                   |\n| 2.  | `CookieStore`  | Stores the locale in a cookie.                      |\n| 3.  | `AppStore`     | Required. Sets the locale as the active app locale. |\n\nUpdate the `stores` array in the config to choose which stores to use.\n\n\u003e You can create your own store by implementing the `CodeZero\\LocalizedRoutes\\Middleware\\Stores\\Store` interface and add a reference to it in the config file. The stores are resolved from Laravel's IOC container, so you can add any dependencies to your constructor.\n\nAlthough no further configuration is needed, you can change advanced settings in the config file.\n\n## 🚘 Register Routes\n\nDefine your routes inside the `Route::localized()` closure, to automatically register them for each locale.\n\nThis will prepend the locale to the route's URI and name.\nIf you configured custom domains, it will use those instead of the URI slugs.\nYou can also use route groups inside the closure.\n\n```php\nRoute::localized(function () {\n    Route::get('about', [AboutController::class, 'index'])-\u003ename('about');\n});\n```\n\nWith supported locales `['en', 'nl']`, the above would register:\n\n| URI         | Name       |\n|-------------|------------|\n| `/en/about` | `en.about` |\n| `/nl/about` | `nl.about` |\n\nAnd with the omitted locale set to `en`, the result would be:\n\n| URI         | Name       |\n|-------------|------------|\n| `/about`    | `en.about` |\n| `/nl/about` | `nl.about` |\n\n\u003e In a most practical scenario, you would register a route either localized or non-localized, but not both.\n\u003e If you do, you will always need to specify a locale to generate the URL with the `route()` helper, because existing route names always have priority.\n\u003e Especially when omitting a main locale from the URL, this would be problematic, because you can't have, for example, a localized `/about` route and a non-localized `/about` route in this case.\n\u003e The same idea applies to the `/` (root) route! Also note that the route names still have the locale prefix even if the slug is omitted.\n\n### ☑ Translate Parameters with Route Model Binding\n\nWhen resolving incoming route parameters from a request, you probably rely on [Laravel's route model binding](https://laravel.com/docs/routing#route-model-binding).\nYou typehint a model in the controller, and it will look for a `{model}` by its ID, or by a specific attribute like `{model:slug}`.\nIf it finds one that matches the parameter value in the URL, it is injected in the controller.\n\n```php\n// Example: use the post slug as the route parameter\nRoute::get('posts/{post:slug}', [PostsController::class, 'index']);\n\n// PostsController.php\npublic function index(Post $post)\n{\n    return $post;\n}\n```\n\nHowever, to resolve a localized parameter you need to add a `resolveRouteBinding()` method to your model.\nIn this method you need to write the logic required to find a match, using the parameter value from the URL.\n\nFor example, you might have a JSON column in your database containing translated slugs:\n\n```php\npublic function resolveRouteBinding($value, $field = null)\n{\n    // Default field to query if no parameter field is specified\n    $field = $field ?: $this-\u003egetRouteKeyName();\n    \n    // If the parameter field is 'slug',\n    // lets query a JSON field with translations\n    if ($field === 'slug') {\n        $field .= '-\u003e' . App::getLocale(); \n    }\n    \n    // Perform the query to find the parameter value in the database\n    return $this-\u003ewhere($field, $value)-\u003efirstOrFail();\n}\n```\n\n\u003e If you are looking for a good solution to implement translated attributes on your models, be sure to check out [spatie/laravel-translatable](https://github.com/spatie/laravel-translatable).\n\n### ☑ Translate Hard-Coded URI Slugs\n\nThis package includes [codezero/laravel-uri-translator](https://github.com/codezero-be/laravel-uri-translator).\nThis registers a `Lang::uri()` macro that enables you to translate individual, hard-coded URI slugs.\nRoute parameters will not be translated by this macro.\n\nRoutes with translated URIs need to have a name in order to generate localized versions of it using the `route()` helper or the `Route::localizedUrl()` macro.\nBecause these routes have different slugs depending on the locale, the route name is the only thing that links them together.\n\nFirst, you create a `routes.php` translation file in your app's `lang` folder for each locale, for example:\n\n```php\nlang/nl/routes.php\nlang/fr/routes.php\n```\n\nThen you add the appropriate translations to each file:\n\n```php\n// lang/nl/routes.php\nreturn [\n    'about' =\u003e 'over',\n    'us' =\u003e 'ons',\n];\n```\n\nAnd finally, you use the macro when registering routes:\n\n```php\nRoute::localized(function () {\n    Route::get(Lang::uri('about/us'), [AboutController::class, 'index'])-\u003ename('about');\n});\n```\n\nThe URI macro accepts 2 additional parameters:\n\n1. A locale, in case you need translations to a locale other than the current app locale.\n2. A namespace, in case your translation files reside in a package.\n\n```php\nLang::uri('hello/world', 'fr', 'my-package');\n```\n\nYou can also use `trans()-\u003euri('hello/world')` instead of `Lang::uri('hello/world')`.\n\n### Example\n\nUsing these example translations:\n\n```php\n// lang/nl/routes.php\nreturn [\n    'hello' =\u003e 'hallo',\n    'world' =\u003e 'wereld',\n    'override/hello/world' =\u003e 'something/very/different',\n    'hello/world/{parameter}' =\u003e 'uri/with/{parameter}',\n];\n```\n\nThese are possible translation results:\n\n```php\n// Translate every slug individually\n// Translates to: 'hallo/wereld'\nLang::uri('hello/world');\n\n// Keep original slug when missing translation\n// Translates to: 'hallo/big/wereld'\nLang::uri('hello/big/world');\n\n// Translate slugs, but not parameter placeholders\n// Translates to: 'hallo/{world}'\nLang::uri('hello/{world}');\n\n// Translate full URIs if an exact translation exists\n// Translates to: 'something/very/different'\nLang::uri('override/hello/world');\n\n// Translate full URIs if an exact translation exists (with placeholder)\n// Translates to: 'uri/with/{parameter}'\nLang::uri('hello/world/{parameter}');\n```\n\n### 🔦 Localize 404 Pages\n\nA standard `404` response has no actual `Route` and does not go through the middleware.\nThis means our middleware will not be able to update the locale and the request can not be localized.\n\nTo fix this, you can register this fallback route at the end of your `routes/web.php` file:\n\n```php\nRoute::fallback(\\CodeZero\\LocalizedRoutes\\Controllers\\FallbackController::class);\n```\n\nBecause the fallback route is an actual `Route`, the middleware will run and update the locale.\n\nThe fallback route is a \"catch all\" route that Laravel provides.\nIf you type in a URL that doesn't exist, this route will be triggered instead of a typical 404 exception.\n\nThe `FallbackController` will attempt to respond with a 404 error view, located at `resources/views/errors/404.blade.php`.\nIf this view does not exist, the normal `Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException` will be thrown.\nYou can configure which view to use by changing the `404_view` entry in the config file.\n\nFallback routes will not apply when:\n\n- your existing routes throw a `404` exception (as in `abort(404)`)\n- your existing routes throw a `ModelNotFoundException` (like with route model binding)\n- your existing routes throw any other exception\n\n## 🗄 Cache Routes\n\nIn production, you can safely cache your routes per usual.\n\n```bash\nphp artisan route:cache\n```\n\n## ⚓ Generate Route URLs\n\n### ☑ Generate URLs for the Active Locale\n\nYou can get the URL of your named routes as usual, using the `route()` helper.\n\n```php\n$url = route('about'); \n```\n\nIf you registered an `about` route that is not localized, then `about` is an existing route name and its URL will be returned.\nOtherwise, this will try to generate the `about` URL for the active locale, e.g. `en.about`.\n\n### ☑ Generate URLs for a Specific Locale\n\nIn some cases, you might need to generate a URL for a specific locale.\nFor this purpose, an additional locale parameter was added to Laravel's `route()` helper.\n\n```php\n$url = route('about', [], true, 'nl'); // this will load 'nl.about'\n```\n\n### ☑ Generate URLs with Localized Parameters\n\nThere are a number of ways to generate route URLs with localized parameters.\n\n#### Pass Localized Parameters Manually\n\nLet's say we have a `Post` model with a `getSlug()` method:\n\n```php\npublic function getSlug($locale = null)\n{\n    $locale = $locale ?: App::getLocale();\n    \n    $slugs = [\n        'en' =\u003e 'en-slug',\n        'nl' =\u003e 'nl-slug',\n    ];\n\n    return $slugs[$locale] ?? '';\n}\n```\n\n\u003e Of course, in a real project the slugs wouldn't be hard-coded.\n\u003e If you are looking for a good solution to implement translated attributes on your models, be sure to check out [spatie/laravel-translatable](https://github.com/spatie/laravel-translatable).\n\nNow you can pass a localized slug to the `route()` function:\n\n```php\nroute('posts.show', [$post-\u003egetSlug()]);\nroute('posts.show', [$post-\u003egetSlug('nl')], true, 'nl');\n```\n\n#### Use a Custom Localized Route Key\n\nYou can let Laravel resolve localized parameters automatically by adding the `getRouteKey()` method to your model:\n\n```php\npublic function getRouteKey()\n{\n    $locale = App::getLocale();\n    \n    $slugs = [\n        'en' =\u003e 'en-slug',\n        'nl' =\u003e 'nl-slug',\n    ];\n\n    return $slugs[$locale] ?? '';\n}\n```\n\nNow you can just pass the model:\n\n```php\nroute('posts.show', [$post]);\nroute('posts.show', [$post], true, 'nl');\n```\n\n### ☑ Fallback URLs\n\nA fallback locale can be provided in the config file.\nIf the locale parameter for the `route()` helper is not a supported locale, the fallback locale will be used instead.\n\n```php\n// When the fallback locale is set to 'en'\n// and the supported locales are 'en' and 'nl'\n\n$url = route('about', [], true, 'nl'); // this will load 'nl.about'\n$url = route('about', [], true, 'wk'); // this will load 'en.about'\n```\n\nIf neither a regular nor a localized route can be resolved, a `Symfony\\Component\\Routing\\Exception\\RouteNotFoundException` will be thrown.\n\n### ☑ Generate Localized Versions of the Current URL\n\nTo generate a URL for the current route in any locale, you can use the `Route::localizedUrl()` macro.\n\n#### Pass Parameters Manually\n\nJust like with the `route()` helper, you can pass parameters as a second argument.\n\nLet's say we have a `Post` model with a `getSlug()` method:\n\n```php\npublic function getSlug($locale = null)\n{\n    $locale = $locale ?: App::getLocale();\n    \n    $slugs = [\n        'en' =\u003e 'en-slug',\n        'nl' =\u003e 'nl-slug',\n    ];\n\n    return $slugs[$locale] ?? '';\n}\n```\n\nNow you can pass a localized slug to the macro:\n\n```php\n$current = Route::localizedUrl(null, [$post-\u003egetSlug()]);\n$en = Route::localizedUrl('en', [$post-\u003egetSlug('en')]);\n$nl = Route::localizedUrl('nl', [$post-\u003egetSlug('nl')]);\n```\n\n#### Use a Custom Route Key\n\nIf you add the model's `getRouteKey()` method, you don't need to pass the parameter at all.\n\n```php\npublic function getRouteKey()\n{\n    $locale = App::getLocale();\n    \n    $slugs = [\n        'en' =\u003e 'en-slug',\n        'nl' =\u003e 'nl-slug',\n    ];\n\n    return $slugs[$locale] ?? '';\n}\n```\n\nThe macro will now automatically figure out what parameters the current route has and fetch the values.\n\n```php\n$current = Route::localizedUrl();\n$en = Route::localizedUrl('en');\n$nl = Route::localizedUrl('nl');\n```\n\n#### Multiple Route Keys\n\nIf you have a route with multiple keys, like `/en/posts/{id}/{slug}`, then you can implement the `ProvidesRouteParameters` interface in your model.\nFrom the `getRouteParameters()` method, you then return the required parameter values.\n\n```php\nuse CodeZero\\LocalizedRoutes\\ProvidesRouteParameters;\nuse Illuminate\\Database\\Eloquent\\Model;\n\nclass Post extends Model implements ProvidesRouteParameters\n{\n    public function getRouteParameters($locale = null)\n    {\n        return [\n            $this-\u003eid,\n            $this-\u003egetSlug($locale) // Add this method yourself of course :)\n        ];\n    }\n}\n```\n\nNow, the parameters will still be resolved automatically:\n\n```php\n$current = Route::localizedUrl();\n$en = Route::localizedUrl('en');\n$nl = Route::localizedUrl('nl');\n```\n\n#### Keep or Remove Query String\n\nBy default, the query string will be included in the generated URL.\nIf you don't want this, you can pass an extra parameter to the macro:\n\n```php\n$keepQuery = false;\n$current = Route::localizedUrl(null, [], true, $keepQuery);\n```\n\n### ☑ Example Locale Switcher\n\nThe following Blade snippet will add a link to the current page in every alternative locale.\n\nIt will only run if the current route is localized or a fallback route.\n\n```blade\n@if (Route::isLocalized() || Route::isFallback())\n    \u003cul\u003e\n        @foreach(LocaleConfig::getLocales() as $locale)\n            @if ( ! App::isLocale($locale))\n                \u003cli\u003e\n                    \u003ca href=\"{{ Route::localizedUrl($locale) }}\"\u003e\n                        {{ strtoupper($locale) }}\n                    \u003c/a\u003e\n                \u003c/li\u003e\n            @endif\n        @endforeach\n    \u003c/ul\u003e\n@endif\n```\n\n## 🖋 Generate Signed Route URLs\n\nGenerating a localized signed route and temporary signed route URL is just as easy as generating normal route URLs.\nPass it the route name, the necessary parameters, and you will get the URL for the current locale.\n\n```php\n$signedUrl = URL::signedRoute('reset.password', ['user' =\u003e $id]);\n$signedUrl = URL::temporarySignedRoute('reset.password', now()-\u003eaddMinutes(30), ['user' =\u003e $id]);\n```\n\nYou can also generate a signed route URL for a specific locale:\n\n```php\n$signedUrl = URL::signedRoute('reset.password', ['user' =\u003e $id], null, true, 'nl');\n$signedUrl = URL::temporarySignedRoute('reset.password', now()-\u003eaddMinutes(30), ['user' =\u003e $id], true, 'nl');\n```\n\nCheck out the [Laravel docs](https://laravel.com/docs/urls#signed-urls) for more info on signed routes.\n\n## 🚌 Redirect to Routes\n\nYou can redirect to routes, just like you would in a normal Laravel app, using the `redirect()` helper or the `Redirect` facade.\n\nIf you register an `about` route that is not localized, then `about` is an existing route name and its URL will be redirected to.\nOtherwise, this will try to redirect to the `about` route for the active locale, e.g. `en.about`:\n\n```php\nreturn redirect()-\u003eroute('about');\n```\n\nYou can also redirect to URLs in a specific locale:\n\n```php\n// Redirects to 'nl.about'\nreturn redirect()-\u003eroute('about', [], 302, [], 'nl');\n```\n\nA localized version of the `signedRoute` and `temporarySignedRoute` redirects are included as well:\n\n```php\n// Redirects to the active locale\nreturn redirect()-\u003esignedRoute('signed.route', ['user' =\u003e $id]);\nreturn redirect()-\u003etemporarySignedRoute('signed.route', now()-\u003eaddMinutes(30), ['user' =\u003e $id]);\n\n// Redirects to 'nl.signed.route'\nreturn redirect()-\u003esignedRoute('signed.route', ['user' =\u003e $id], null, 302, [], 'nl');\nreturn redirect()-\u003etemporarySignedRoute('signed.route', now()-\u003eaddMinutes(30), ['user' =\u003e $id], 302, [], 'nl');\n```\n\n## 🪧 Automatically Redirect to Localized URLs\n\nTo redirect any non-localized URL to its localized version, you can set the config option `redirect_to_localized_urls` to `true`, and register the following fallback route with the `FallbackController` at the end of your `routes/web.php` file.\n\n```php\nRoute::fallback(\\CodeZero\\LocalizedRoutes\\Controllers\\FallbackController::class);\n```\n\nThe fallback route is a \"catch all\" route that Laravel provides.\nIf you type in a URL that doesn't exist, this route will be triggered instead of a typical 404 exception.\n\nThe `FallbackController` will attempt to redirect to a localized version of the URL, or return a [localized 404 response](#-localize-404-pages) if it doesn't exist.\n\nFor example:\n\n| URI      | Redirects To |\n|----------|--------------|\n| `/`      | `/en`        |\n| `/about` | `/en/about`  |\n\nIf the omitted locale is set to `en`:\n\n| URI         | Redirects To |\n|-------------|--------------|\n| `/en`       | `/`          |\n| `/en/about` | `/about`     |\n\nIf a route doesn't exist, a `404` response will be returned.\n\n## 🪜 Helpers\n\n### `Route::hasLocalized()`\n\n```php\n// Check if a named route exists in the active locale:\n$exists = Route::hasLocalized('about');\n// Check if a named route exists in a specific locale:\n$exists = Route::hasLocalized('about', 'nl');\n```\n\n### `Route::isLocalized()`\n\n```php\n// Check if the current route is localized:\n$isLocalized = Route::isLocalized();\n// Check if the current route is localized and has a specific name:\n$isLocalized = Route::isLocalized('about');\n// Check if the current route has a specific locale and has a specific name:\n$isLocalized = Route::isLocalized('about', 'nl');\n// Check if the current route is localized and its name matches a pattern:\n$isLocalized = Route::isLocalized(['admin.*', 'dashboard.*']);\n// Check if the current route has one of the specified locales and has a specific name:\n$isLocalized = Route::isLocalized('about', ['en', 'nl']);\n```\n\n### `Route::isFallback()`\n\n```php\n// Check if the current route is a fallback route:\n$isFallback = Route::isFallback();\n```\n\n## 🚧 Testing\n\n```bash\ncomposer test\n```\n\n## ☕ Credits\n\n- [Ivan Vermeyen](https://github.com/ivanvermeyen)\n- [All contributors](https://github.com/codezero-be/laravel-localized-routes/contributors)\n\n## 🔒 Security\n\nIf you discover any security related issues, please [e-mail me](mailto:ivan@codezero.be) instead of using the issue tracker.\n\n## 📑 Changelog\n\nA complete list of all notable changes to this package can be found on the\n[releases page](https://github.com/codezero-be/laravel-localized-routes/releases).\n\n## 📜 License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodezero-be%2Flaravel-localized-routes","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcodezero-be%2Flaravel-localized-routes","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodezero-be%2Flaravel-localized-routes/lists"}