Hi, in this post, we will learn how to set up and use localized routes in a Laravel app.

For this tutorial we will use this package codezero-be/laravel-localized-routes.

📦 Install

let’s install the package

Next, we have to publish configuration file

now in the config folder you will find a localized-routes.php file.

 

♣️ Configure

 

⚫️ Supported Locales

➖Using Slug

Add any locales you wish to support to your published config/localized-routes.php file:

➖Using Domains

Alternatively, you can use a different domain or subdomain for each locale by configuring the supported-locales like this:

 

⚫️Omit Slug for Main Locale

Specify your main locale if you want to omit its slug from the URL:

Setting this option to 'en' will result, for example, in URL’s like this:

  • English: /some-url instead of the default /en/some-url
  • Arabic: /ar/some-url as usual
  • French: /fr/some-url as usual

This option has no effect if you use domains instead of slugs.

 

⚫️Use Middleware to Update App Locale

By default, the app locale will always be what you configured in config/app.php. To automatically update the app locale when a localized route is accessed, you need to use a middleware.

Read Also  Laravel .env variable in Blade and Controller

To make route model binding work in Laravel 6+ you always also need to add the middleware to the $middlewarePriority array in app/Http/Kernel.php so it runs before SubstituteBindings:

Enable the middleware by Simply setting the option to true to add the middleware to every localized route:

 

♣️ Register Routes

Example:

In the above example there are 10 routes being registered. If configured custom domains, it will use those instead of the slugs.

URIName
/not-localizednot_localized
/enen.index
/arar.index
/frfr.index
/en/contacten.contact
/ar/contactar.contact
/fr/contactfr.contact
/en/admin/homeen.admin.home
/ar/admin/homear.admin.home
/fr/admin/homefr.admin.home

 

If you set omit_url_prefix_for_locale to 'en' in the configuration file, the resulting routes look like this:

Read Also  Integrate TinyMCE editor in Laravel with a File Manager / Image Upload
URIName
/not-localizednot_localized
/en.index
/arar.index
/frfr.index
/contacten.contact
/ar/contactar.contact
/fr/contactfr.contact
/admin/homeen.admin.home
/ar/admin/homear.admin.home
/fr/admin/homefr.admin.home

 

♣️ Generate Route URL’s

You can get the URL of your named routes as usual, using the route() helper.

This is the new route helper signature:

Example:

 

♣️ Redirect to Routes

Laravel’s Redirector uses the same UrlGenerator as the route() function behind the scenes. Because it’s overriding this class, you can easily redirect to your routes.

You can’t redirect to URL’s in a specific locale this way, but if you need to, you can of course just use the route() function.

 

That’s it !, for more features you can check documentation of the package codezero-be/laravel-localized-routes.


    No Comments

    Leave a Reply

    your email address will not be published. required fields are marked *

    *
    *