在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):LaurentEsc/Laravel-Subdomain-Localization开源软件地址(OpenSource Url):https://github.com/LaurentEsc/Laravel-Subdomain-Localization开源编程语言(OpenSource Language):PHP 100.0%开源软件介绍(OpenSource Introduction):Important notice: this package is no longer maintained. Laravel-Subdomain-LocalizationSubdomain localization support for Laravel. Table of ContentsInstallationComposerAdd Laravel-Subdomain-Localization to your
Run ManuallyIt's recommended that you use Composer, however you can download and install from this repository. LaravelLaravel-Subdomain-Localization comes with a service provider for Laravel. To register the service provider in your Laravel application, open ...
LaurentEsc\Localization\LocalizationServiceProvider::class
... Laravel-Subdomain-Localization comes with 2 facades: If you want to use them, open ...
'Localize' => LaurentEsc\Localization\Facades\Localize::class,
'Router' => LaurentEsc\Localization\Facades\Router::class,
... Laravel comes with a middleware that can be used to enforce the use of a language subdomain. If you want to use it, open ...
'localize' => \LaurentEsc\Localization\Middleware\Localization::class,
... UsageLocale detectionOpen ...
use LaurentEsc\Localization\Facades\Localize;
...
public function boot(Router $router)
{
// This will guess a locale from the current HTTP request
// and set the application locale
Localize::detectLocale();
parent::boot($router);
}
... Once you have done this, there is nothing more that you MUST do. Laravel application locale has been set and you can use other locale-dependant Laravel components (e.g. Translation) as you normally do. MiddlewareIf you want to enforce the use of a language subdomain for some routes, you can simply assign the middleware provided, for example as follows in // Without the localize middleware, this route can be reached with or without language subdomain
Route::get('logout', 'AuthController@logout');
// With the localize middleware, this route cannot be reached without language subdomain
Route::group([ 'middleware' => [ 'localize' ]], function() {
Route::get('welcome', 'WelcomeController@index');
}); For more information about Middleware, please refer to Laravel docs. Route translationIf you want to use translated routes (en.yourdomain.com/welcome, fr.yourdomain.com/bienvenue), proceed as follows: First, create language files for the languages that you support:
return [
// route name => route translation
'welcome' => 'welcome',
'user_profile' => 'user/{username}',
];
return [
// route name => route translation
'welcome' => 'bienvenue',
'user_profile' => 'utilisateur/{username}',
]; Then, here is how you define translated routes in Route::group([ 'middleware' => [ 'localize' ]], function() {
Route::get(Router::resolve('routes.welcome'), 'WelcomeController@index');
}); You can of course name the language files as you wish, and pass the proper prefix (routes. in the example) to the resolve() method. ConfigurationConfiguration fileIn order to edit the default package configuration, you can run the following artisan command:
Once you have done that, you will find the config file at Configuration values
An array of the locales accepted by the routing system.
Use this option to enable or disable the use of cookies during the locale detection.
Use this option to enable or disable the use of the browser settings during the locale detection.
Here you may change the name of the cookie used to save the locale. This option is used only if localization with cookie is enabled.
Here you may change the name of the domain used in your application. By default, the domain is read from the .env file. Useful functionsThe package provides useful functions that you can use - for example - in your views: Translate current URL <a href="{{ Router::current('fr') }}">See the french version</a> Use Get alternate versions of the current URL @foreach (Router::getCurrentVersions() as $locale => $url)
<a href="{{ $url }}">{{ $locale }}</a>
@endforeach Use You can pass Get localized version for a given route <a href="{{ Router::url('user_profile', [ 'username' => 'JohnDoe' ], 'fr') }}">See JohnDoe's profile</a> Use You can pass route parameters if necessary. If you don't give a specific locale, it will use the current locale. ChangelogTo see what has changed in recent versions, see the CHANGELOG. LicenseThis package is licensed under the MIT license. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论