开源软件名称(OpenSource Name):BenConstable/laravel-localize-middleware开源软件地址(OpenSource Url):https://github.com/BenConstable/laravel-localize-middleware开源编程语言(OpenSource Language):PHP 100.0%开源软件介绍(OpenSource Introduction):Laravel Localize Middleware
This package provides a simple set of configuration and middleware to allow you to automatically set your application's locale using the current request. You can set the locale from a request parameter, header, the current host, a cookie or session data. InstallationInstall the package via Composer:
Next, add the package's service provider to your // config/app.php
'providers' => [
BenConstable\Localize\LocalizeServiceProvider::class
] and then you'll just need to publish the package's configuration:
which will create UsageOut-of-the-box, the package is configured to set the application locale using a
request parameter called // app/Http/Kernel.php
protected $middleware = [
\BenConstable\Localize\Http\Middleware\Localize::class
]; It's recommended to set this middleware globally and early in the stack, but you're free to register it in whatever way that suits your needs. ConfigurationConfiguration can be found at The list of available determiners is shown below. Determining the locale from a request parameterDriver name: The default determiner sets the application locale from a request parameter
called The parameter will be discovered in the query string or request body. Determining the locale from a request headerDriver name: This determiner sets the application locale from a request header, which defaults
to Aside: For information on using Determining the locale using the current hostDriver name: This determiner sets a different application locale depending on the current host.
You'll need to set a map of your application's locales to hosts using the Determining the locale from a cookieDriver name: This determiner sets the application locale from a cookie called Determining the locale from the sessionDriver name: This determiner sets the application locale from a session value called Using multiple determinersSometimes it might be useful to try and determine the locale from more than one
source. If you'd like to do this, just set the 'driver' => [
'cookie',
'parameter'
] The locale will then be deteremined from whichever determiner first provides a successful match, so make sure you add the drivers in the correct order (earliest in the array will be used first). Determining the locale outside of middlewareYou don't have to use the provided middleware if you don't want to. You can instead write your own, or avoid using middleware entirely. To determine the locale in your own code, first register an alias for the provided
facade (which is actually a reference to // config/app.php
'aliases' => [
'Localizer' => BenConstable\Localize\DeterminerFacade::class
] Then, you can just do: $locale = Localizer::determineLocale($request); to determine the locale and do with it what you like. ContributingSee CONTRIBUTING.md. Other Localization ProjectsHere are some other Laravel localization projects that might be useful to you: LicenseMIT © Ben Constable 2017. See LICENSE for more info. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论