简体   繁体   English

Laravel本地化url帮助器

[英]Laravel localize url helper

I need to localize my website. 我需要本地化我的网站。 I am using this package: https://github.com/mcamara/laravel-localization My route group is: 我正在使用这个包: https//github.com/mcamara/laravel-localization我的路由组是:

Route::group(['prefix' => LaravelLocalization::setLocale()], function()
{
Route::get('/garage', ['as' => 'garage', 'uses' => 'GarageController@garage']);
//OTHER ROUTES
});

And i want to localize link to this route. 我想本地化这条路线的链接。 If i am using 如果我正在使用

href="{{ route('garage') }}

everything is ok, link looks like "www.example.com/locale/garage". 一切正常,链接看起来像“www.example.com/locale/garage”。 But if i am using 但如果我正在使用

href="{{ url('/garage') }}

my localization doesn't work, link looks like "www.example.com/garage". 我的本地化不起作用,链接看起来像“www.example.com/garage”。 Is there some way to localize links made with URL helper? 有没有办法本地化使用URL帮助程序创建的链接?

LaravelLocalization package includes a middleware object to redirect all non-localized routes to the corresponding "localized". LaravelLocalization包中包含一个中间件对象,用于将所有非本地化路由重定向到相应的“本地化”。

So, if a user navigates to http://www.example.com/test and the system has this middleware active and 'en' as the current locale for this user, it would redirect him automatically to http://www.example.com/en/test . 因此,如果用户导航到http://www.example.com/test并且系统将此中间件激活并且“en”作为此用户的当前区域设置,则会自动将其重定向到http://www.example.com/en/test

To active this functionality go to app/Http/Kernel.php and add these classes in protected $routeMiddleware = [] at the beginning and then in your routes file bring these changes: 要激活此功能,请转到app/Http/Kernel.php并在开头的protected $routeMiddleware = []中添加这些类,然后在路由文件中添加以下更改:

Route::group([
'prefix' => LaravelLocalization::setLocale(),
'middleware' => [ 'localeSessionRedirect', 'localizationRedirect', 'localeViewPath' ]
], function()
{
Route::get('/garage', ['as' => 'garage', 'uses' => 'GarageController@garage']);
//OTHER ROUTES
});

And your problem will be solved. 而你的问题将得到解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 更改 Laravel 中的资产助手 URL - Change the asset helper url in Laravel Laravel:我应该在辅助程序中在哪里声明“ url”? - Laravel: where should I declare 'url' in helper? Laravel URL助手:如何使用查询参数和哈希值生成完美的URL - Laravel URL helper: How to generate a perfect URL with query parameters and hash 如何使用Laravel的URL生成器帮助器生成的URL不在结尾处? - How to generate a url with Laravel's url generator helper with parameters that are not at the end? LARAVEL:使用 URL HELPER 方法时从 url 中删除了 http - LARAVEL : http removed from url when using URL HELPER method 如何在Laravel视图中将数据库中保存的URL插入URL助手? - How to insert URL saved in database to URL helper in Laravel view? Laravel:如何将当前url作为带参数的视图助手 - Laravel: How to get the current url as a view helper with parameters 如何使用Laravel 5 url()刀片助手提供https链接 - How to serve https links with Laravel 5 url() blade helper Laravel如何在控制器中获取Helper函数的返回值作为URL路径 - Laravel How to get Helper function return value in controller as url path URL 中的 Laravel 请求助手以获取 ID/资源 ID - Laravel request helper in URL to get ID / Resource ID
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM