|
| 1 | +# Laravel Localizer |
| 2 | + |
| 3 | +[]() |
| 4 | +[]() |
| 5 | +[](https://scrutinizer-ci.com/g/codezero-be/laravel-localizer/build-status/master) |
| 6 | +[](https://scrutinizer-ci.com/g/codezero-be/laravel-localizer/?branch=master) |
| 7 | +[](https://scrutinizer-ci.com/g/codezero-be/laravel-localizer/?branch=master) |
| 8 | +[](https://packagist.org/packages/codezero/laravel-localizer) |
| 9 | + |
| 10 | +#### Automatically detect and set an app locale that matches your visitor's preference. |
| 11 | + |
| 12 | +- Define your supported locales and match your visitor's preference |
| 13 | +- Uses the most common locale [detectors](#detectors) by default |
| 14 | +- Uses the most common locale [stores](#stores) by default |
| 15 | +- Easily create and add your own detectors and stores |
| 16 | + |
| 17 | +## Requirements |
| 18 | + |
| 19 | +- PHP >= 7.1 |
| 20 | +- Laravel >= 5.6 |
| 21 | + |
| 22 | +## Install |
| 23 | + |
| 24 | +```php |
| 25 | +composer require codezero/laravel-localizer |
| 26 | +``` |
| 27 | + |
| 28 | +Laravel will automatically register the ServiceProvider. |
| 29 | + |
| 30 | +#### Add Middleware |
| 31 | + |
| 32 | +Add the middleware to the `web` middleware group in `app/Http/Kernel.php` after `StartSession`: |
| 33 | + |
| 34 | +```php |
| 35 | +protected $middlewareGroups = [ |
| 36 | + 'web' => [ |
| 37 | + //... |
| 38 | + \Illuminate\Session\Middleware\StartSession::class, |
| 39 | + //... |
| 40 | + \CodeZero\Localizer\Middleware\SetLocale::class, |
| 41 | + ], |
| 42 | +]; |
| 43 | +``` |
| 44 | + |
| 45 | +#### Publish Configuration File |
| 46 | + |
| 47 | +```php |
| 48 | +php artisan vendor:publish --provider="CodeZero\Localizer\LocalizerServiceProvider" --tag="config" |
| 49 | +``` |
| 50 | + |
| 51 | +You will now find a `localizer.php` file in the `config` folder. |
| 52 | + |
| 53 | +#### Configure Supported Locales |
| 54 | + |
| 55 | +Add any locales you wish to support to your published `config/localizer.php` file: |
| 56 | + |
| 57 | +```php |
| 58 | +'supported-locales' => ['en', 'nl', 'fr']; |
| 59 | +``` |
| 60 | + |
| 61 | +## Drivers |
| 62 | + |
| 63 | +#### Detectors |
| 64 | + |
| 65 | +By default the middleware will use the following detectors for a supported locale: |
| 66 | + |
| 67 | +1. The URL |
| 68 | +2. The session |
| 69 | +3. A cookie |
| 70 | +4. The browser |
| 71 | +5. The app's default locale |
| 72 | + |
| 73 | +If you publish the configuration file, you can choose which detectors to run and in what order. |
| 74 | + |
| 75 | +You can also create your own detector by implementing the `\CodeZero\Localizer\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. |
| 76 | + |
| 77 | +#### Stores |
| 78 | + |
| 79 | +The first supported locale that is returned by a detector will then be stored in: |
| 80 | + |
| 81 | +- The session |
| 82 | +- A cookie |
| 83 | +- The app locale |
| 84 | + |
| 85 | +If you publish the configuration file, you can choose which stores to use. |
| 86 | + |
| 87 | +You can also create your own store by implementing the `\CodeZero\Localizer\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. |
| 88 | + |
| 89 | +## Testing |
| 90 | + |
| 91 | +``` |
| 92 | +composer test |
| 93 | +``` |
| 94 | + |
| 95 | +## Security |
| 96 | + |
| 97 | +If you discover any security related issues, please [e-mail me ](mailto:[email protected]) instead of using the issue tracker. |
| 98 | + |
| 99 | +## Changelog |
| 100 | + |
| 101 | +See a list of important changes in the [changelog](CHANGELOG.md). |
| 102 | + |
| 103 | +## License |
| 104 | + |
| 105 | +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. |
0 commit comments