Download the PHP package outerweb/localization without Composer
On this page you can find all versions of the php package outerweb/localization. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download outerweb/localization
More information about outerweb/localization
Files in outerweb/localization
Package localization
Short Description This package adds multi-language support to your Laravel application.
License MIT
Homepage https://github.com/outer-web/localization
Informations about the package localization
Localization
This package adds multi-language support to your Laravel application:
- Multiple locales configuration
- Localized routes
- Translatable route segments
- Automatic user locale detection
Installation
You can install the package via composer:
Add the Outerweb\Localization\Http\Middleware\SetLocale
middleware to the web
middleware group in app/Http/Kernel.php
:
This will automatically set the locale for each request by going through the following steps:
- Check if the locale is set in the URL (e.g.
http://example.com/en
) - Check if the locale is set in a cookie
- Check for a matching locale in the preferred languages of the user's browser
- Use the fallback locale
You can publish the config file with:
This is the contents of the published config file:
Usage
Defining routes
Define the routes you want to localize like this:
This will then generate the home route for each locale you have defined in the supported_locales
config value.
For example, if you have defined en
and nl
as supported locales, the following routes will be generated:
http://example.com/en
(route name:en.home
)http://example.com/nl
(route name:nl.home
)
You can also define a fallback route that will redirect to the localized route:
Translating route segments
You can translate each route segment by adding it to the configured translations file in the lang
directory.
For example, if you defined a route /about-us
and you support the locales en
and nl
,
you can add this to the configured translations files:
In lang/en/routes.php
:
In lang/nl/routes.php
:
This will then generate the following routes:
http://example.com/en/about-us
(route name:en.about-us
)http://example.com/nl/over-ons
(route name:nl.about-us
)
Generating localized URLs
When using localized routes, you cannot use the route()
helper to generate URLs.
Instead, you can use the localizedRoute()
helper:
As you can see above, this one takes the same parameters as the route()
helper.
Getting the localized routes for the current route
You can get the localized routes for the current route like this:
This will for example return the following array if you are on the 'about-us' page:
This can be useful if you want to generate a language switcher.
Getting the localized routes for a specific route
You can get the localized routes for a specific route like this:
In this example, the result would be:
This can be useful if you want to tell Google about the other localized versions of a page. (See this article for more information)
Changelog
Please see CHANGELOG for more information on what has changed recently.
Credits
- Simon Broekaert
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
All versions of localization with dependencies
laravel/framework Version ^10.0|^11.0
spatie/laravel-package-tools Version ^1.16