【问题标题】:Laravel multilingual site and routesLaravel 多语言站点和路线
【发布时间】:2017-02-21 12:20:36
【问题描述】:

我有一个五种语言的网站,每个页面的所有 URL 都翻译成每种语言,我将使用 Laravel 5.3 对其进行更新。

我已按照this 教程将以下多语言语言环境添加到我的项目中:

'locales' => ['de' => 'German', 'en' => 'English', 'fr' => 'French', 'it' => 'Italian', 'es' => 'Spanish']

这可能是web.php 文件中Contact Us 页面路由的示例:

Route::get('/kontakt', 'ContactController@index');
Route::get('/contact', 'ContactController@index');
Route::get('/contactez', 'ContactController@index');
Route::get('/contattaci', 'ContactController@index');
Route::get('/contacto', 'ContactController@index');

但如果我在浏览器中输入: http://localhost/myproject/en/contattacihttp://localhost/myproject/en/kontakt

我可以访问“联系人”视图,这不应该发生,应该使用 en 区域设置 contact/en/contactcontattaci 与意大利语 (/it/contattaci) 和 kontakt 与德语 (/de/kontakt) 等。

有人知道为什么会发生这种情况,或者哪种方法是创建不同翻译路线的正确方法?

【问题讨论】:

  • 看看here
  • @Moppo 我很久以前就看过那个帖子,但对我没有用。但我试图再次遵循它,并且工作! :) 我想要de 前缀作为我的默认语言环境,所以我也将它添加到alt_langs 数组中,但我现在需要的是重定向到/de,当只输入/ 时。我怎样才能做到这一点?

标签: php laravel routes multilingual


【解决方案1】:

您可能没有指定完整的路由路径。试试这样的:

// EN routes
Route::group(['prefix' => 'en'], function() {
    Route::get('contact', 'ContactController@index');
    //... other EN routes
});
// FR routes
Route::group(['prefix' => 'fr'], function() {
    Route::get('contact', 'ContactController@index');
    //... other FR routes
});

但您应该查看@Moppo 的链接,因为有更简单的方法来管理本地化路线。

【讨论】:

  • 你可以尝试用和ID屏蔽路由器,并将ID与不同的语言联系起来......
  • 我试过这样做,但没有成功。我按照@Moppo 建议我的教程进行操作,并且成功了。但在我的情况下,默认语言环境是/de,当只输入/ 时我需要重定向到它。你知道我怎样才能做到这一点吗? ——
  • Route::get('/', function() { return redirect('/de'); });
  • Moppos 链接在哪里?
猜你喜欢
  • 1970-01-01
  • 2013-06-14
  • 1970-01-01
  • 2015-07-19
  • 2017-08-14
  • 1970-01-01
  • 1970-01-01
  • 2013-07-10
  • 2013-09-13
相关资源
最近更新 更多