问题描述
当前,我的系统使用2种语言,分别是英语和德语.我的目标是通过在提到的语言之间进行切换来浏览以下路线-
Currently, my system is using 2 languages which is English and German. my goal is to browse following routes by switching between the mentioned language -
base-url/contact - for english
base-url/kontakte - for german
当前,我需要在资源文件夹中放置路由文件,并在其中放置了必要的翻译词.
Currently, I have required routes file in the resource folder, where I have put the necessary translated words.
resources/lang/en/routes.php
resources/lang/de/routes.php
当前在web.php中-
In web.php I have currently -
Route::get(r('contact'), 'TestController@index')->name('contact');
通过r()辅助函数,我得到了活动的翻译单词.
by r() helper function I am getting the active translated word.
在我的用户表上,当我从用户个人资料更新语言时,我在语言环境"列中存储活动语言-
On my user table, I have locale column where I am storing the active language when I am updating the language from user profile -
\Session::put('locale', $request->input('locale'));
我已经创建了一个中间件本地化版本-
I have created a middleware Localization where I have currently -
public function handle($request, Closure $next)
{
if ( \Session::has('locale')) {
\App::setLocale(\Session::get('locale'));
Carbon::setLocale(\Session::get('locale'));
}
return $next($request);
}
当前,该代码对于刀片翻译的单词运行正常.但翻译的路线不起作用.每当我切换并访问任何路线时,都会出现404错误.但是,如果我通过PHP artisan serve重新启动服务器,则可以使用更改的语言.
Currently, the code is working fine for blade translated word. but the translated routes are not working. whenever I switch and visit any route, it gives me 404 error. but if I restart the server by PHP artisan serve, it works with changed language.
那么如何解决该问题?
推荐答案
:)尝试将\App::setLocale(\Session::get('locale')
放在路由文件(routes.php或web.php/api.php)的开头
:) Try to put \App::setLocale(\Session::get('locale')
in the beginning of the routes file (routes.php, or web.php/api.php)
这篇关于没有前缀的Laravel多语言路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!