Laravel本地化的一致性

问题描述 投票:1回答:2

我正在构建一个laravel应用程序。我正在构建一个多语言应用程序,它将具有法语和西班牙语,其URL将是

www.example.com/fr/route/slug for french 
www.example.com/es/route/slug for spanish 
www.example.com/route/slug for english which is the main one

但是我很困惑,我应该如何在同一种语言中保持从一个URL到另一个URL的一致性,即当我点击法语链接时,应该返回的应该仍然是法语。例如:

from 
www.example.com/fr/route/slug1  to 
www.example.com/fr/route/another_path to
www.example.com/fr/route/final_path which would be maintaing same language path

www.example.com/es/route/slug1 to 
www.example.com/es/route/another_path to
www.example.com/es/route/final_path for spanish 

www.example.com/route/slug1 to 
www.example.com/route/another_path to 
www.example.com/route/final_path for english 

当我从例如法语到英语页面必须一致,例如

www.example.com/fr/route/slug1 to 
www.example.com/fr/route/another_path in french to english
www.example.com/route/another_path

我应该采取哪些步骤?任何帮助,将不胜感激。提前致谢。

laravel localization consistency
2个回答
0
投票

为了保持这一点,您可以按照以下步骤操作:

  1. 在您网站的语言更改点(例如,从下拉菜单更改语言),在会话中存储选定的值,然后您的页面应重新加载
  2. 然后,您必须创建一个中间件来管理本地化。
  3. 您只需在中间件的handle方法中编写几行代码即可。 $ lang = Session :: get(“language”);应用:: setLocale的($ LANG);
  4. 多数民众赞成,这将管理您的一致性。

0
投票

您应该为所有路线添加此组

Route::group(['middlewareGroups' => 'web', 'prefix' => '{local}'], function () {

然后制作中间件,然后抓住本地,然后:

public function handle($request, Closure $next)
    {
    App::setLocale($locale);
}

并通过将其添加到位于Http / kernel.php文件中的$ middleware数组中来应用它

© www.soinside.com 2019 - 2024. All rights reserved.