如何翻译Laravel的路线?(How to translate routes in Laravel?)

编程入门 行业动态 更新时间:2024-10-26 09:26:01
如何翻译Laravel的路线?(How to translate routes in Laravel?)

基于这个问题,我试图用英语和西班牙语翻译Laravel 4.2的路线。

这是我的应用程序/ routes.php代码:

<?php /*Set up locale and locale_prefix if other language is selected*/ if (in_array(Request::segment(1), Config::get('app.web_config.alt_langs'))) { App::setLocale(Request::segment(1)); Config::set('app.web_config.locale_prefix', Request::segment(1)); } foreach(Lang::get('routes') as $k => $v) { Route::pattern($k, $v); } Route::group(array('prefix' => Config::get('app.web_config.locale_prefix')), function() { Route::get('/{revelar_fotos}/',['as' => 'revelado_online', 'uses' => 'WebController@reveladoOnline'], function(){ return 'pages.revelado_online'.App::getLocale(); }); });

通过点击链接,我得到这个错误的网址:

http://mywebsite.dev/%7Brevelar_fotos%7D

代替:

http://mywebsite.dev/photograph-development

这是我的en / routes.php:

<?php return array( 'revelar_fotos' => 'photograph-development', );

这是我的es / routes.php:

<?php return array( 'revelar_fotos' => 'revelado-online', );

为什么我返回这个错误?

Based on this question I'm trying to translate routes in Laravel 4.2 in english and spanish.

This is my app/routes.php code:

<?php /*Set up locale and locale_prefix if other language is selected*/ if (in_array(Request::segment(1), Config::get('app.web_config.alt_langs'))) { App::setLocale(Request::segment(1)); Config::set('app.web_config.locale_prefix', Request::segment(1)); } foreach(Lang::get('routes') as $k => $v) { Route::pattern($k, $v); } Route::group(array('prefix' => Config::get('app.web_config.locale_prefix')), function() { Route::get('/{revelar_fotos}/',['as' => 'revelado_online', 'uses' => 'WebController@reveladoOnline'], function(){ return 'pages.revelado_online'.App::getLocale(); }); });

By clicking on the link I get this error in the url:

http://mywebsite.dev/%7Brevelar_fotos%7D

Instead of:

http://mywebsite.dev/photograph-development

This is my en/routes.php:

<?php return array( 'revelar_fotos' => 'photograph-development', );

And this is my es/routes.php:

<?php return array( 'revelar_fotos' => 'revelado-online', );

Why I returned this error?

最满意答案

我知道你的问题是与生成的URL http://mywebsite.dev/%7Brevelar_fotos%7D

在这个问题中,你只描述了你如何设置路由处理,这可能工作。 然而,生成链接本身在其他地方完成,并且在那里你还没有完成替换。

你必须生成这样的链接

URL::to(trans("revelar_fotos"));

trans("revelar_fotos")会为您提供正确的路径, URL::to()生成完整链接。

I understand that you problem is with generating the URL http://mywebsite.dev/%7Brevelar_fotos%7D

In the question you only described how you did set up the routing handling and this might work. However generating the link itself is done somewhere else and in there you have not done the substitution.

You must be generating the link something like this

URL::to(trans("revelar_fotos"));

trans("revelar_fotos") will get you the correct path and URL::to() generates full link.

更多推荐

本文发布于:2023-08-04 16:37:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1417828.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路线   Laravel   routes   translate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!