Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tyler36/localization
Localization middlewares
https://github.com/tyler36/localization
i18n laravel laravel-package localization middleware
Last synced: about 5 hours ago
JSON representation
Localization middlewares
- Host: GitHub
- URL: https://github.com/tyler36/localization
- Owner: tyler36
- License: mit
- Created: 2018-06-07T03:05:33.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2021-09-21T06:04:57.000Z (about 3 years ago)
- Last Synced: 2024-04-18T22:04:17.195Z (6 months ago)
- Topics: i18n, laravel, laravel-package, localization, middleware
- Language: PHP
- Homepage:
- Size: 33.2 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# Introduction
This package is designed to simplify multi-locale applications.
Simply add the required middleware and update your configuration.## Installation
- Install package
```
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.- 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)## Middleware
### Option 1: Header request
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.- Register
```\Tyler36\Localization\Middleware\HeaderLocale::class```- 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:
```
// 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'.### Option 2: Query String
This middleware is designed to use a query string.- Register
```\Tyler36\Localization\Middleware\QueryStringLocale::class```- Add the query string to a URL.
By default, the query string is set to ```lang``` but can be changed through the config file.
```
// 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.- Register
```\Tyler36\Localization\Middleware\MemberLocale::class```- Create the attribute on User models
By default, the query string is set to ```locale``` but can be changed through the config file.
```
$table->string('locale', 2)->default(app()->getLocale());
```### Option 4: Session
This middleware looks for a value in Laravel session.- Register
```\Tyler36\Localization\Middleware\SessionLocale::class```- 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.
```
$key = config('localization.session_key', 'locale');
session([$key => request()->get($key)]);
```
or you could use the included controller by updating your route file
```
// 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]
```