Introduction
This package is designed to simplify multi-locale applications.
Simply add the required middleware and update your configuration., (*1)
Installation
composer require tyler36/localization
- Publish configuration
Run the command below to publish the configuration file
php artisan vendor:publish --provider=tyler36/localization
-
Add supported languages
By default, only the current app locale is deemed valid.
You can add additional languages to configuration file (config/localization.php
). Can't see the file? Perhaps you forgot to publish it; see the above step. For a list of internationally recognized language codes, see https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes., (*2)
-
Apply middleware
Add the desired middle to the app/Http/Kernel.php
file in the required location. For more details, see the Laravel documentation (https://laravel.com/docs/master/middleware#registering-middleware), (*3)
Middleware
This middleware uses a request header to set the locale. By default, the query string is set to X-localization
but can be changed through the config file. It's ideal for making request via javascript., (*4)
-
Register
\Tyler36\Localization\Middleware\HeaderLocale::class
, (*5)
-
Set the header in via vanilla javascript or preferred javascript framework. For example, you can apply the header to all Axios
request with the following:, (*6)
// Set language to German ('de')
window.axios = axios;
window.axios.defaults.headers.common['X-localization'] = 'de';
Now, when you send a request to the endpoint Laravel will update the locale to match the setting, assuming it is configured as 'valid'., (*7)
Option 2: Query String
This middleware is designed to use a query string., (*8)
-
Register
\Tyler36\Localization\Middleware\QueryStringLocale::class
, (*9)
-
Add the query string to a URL.
By default, the query string is set to lang
but can be changed through the config file., (*10)
// This will return the site with default locale
https://cool-site.dev
// This will return the site with locale to to German (de)
https://cool-site.dev?lang=de
Option 3: Member preference
This middleware uses an attribute on the User model to set the locale. When a member is logged in, it check their saved locale and applies it., (*11)
-
Register
\Tyler36\Localization\Middleware\MemberLocale::class
, (*12)
-
Create the attribute on User models
By default, the query string is set to locale
but can be changed through the config file., (*13)
$table->string('locale', 2)->default(app()->getLocale());
Option 4: Session
This middleware looks for a value in Laravel session., (*14)
-
Register
\Tyler36\Localization\Middleware\SessionLocale::class
, (*15)
-
Create a route that saves the session_key => locale
to the session
By default, the session_key
is set to locale
but can be changed through the config file., (*16)
$key = config('localization.session_key', 'locale');
session([$key => request()->get($key)]);
or you could use the included controller by updating your route file, (*17)
// routes/web.php
Route::get('/locale/{locale}', [
'as' => 'localization.session',
'uses' => '\Tyler36\Localization\Controller\LocaleController@session'
]);
// blade - create links
route('localization.session', [config('localization.session_key') => $locale]