我在Google中搜索了一个解决方案,但没有找到任何具体的解决方案。我是Laravel的新手,所以...我将解释我的想法,然后将其粘贴到我的代码中。
在页面的页眉中,我有一些用于切换语言的链接。实际上,我有四个国家/地区的旗帜,因为每个旗帜都针对特定的语言做出响应。 我想以用户单击德语标志为例来更改语言。然后需要重新加载页面以更新页面上的翻译。。此功能仅在索引操作中起作用。
我的源代码:这是位于 app.blade.php
中的标头中的html。<div class="languages">
@if (Config::get('app.locale') == 'bg')
<a class="lang bg active" data-lang="bg" href="#">
<img src="{{URL::asset('/images/bg.png')}}" alt="bg" height="14" width="20">
</a>
@else
<a class="lang bg" data-lang="bg" href="#">
<img src="{{URL::asset('/images/bg.png')}}" alt="bg" height="14" width="20">
</a>
@endif
@if (Config::get('app.locale') == 'en')
<a class="lang en active" data-lang="en" href="#">
<img src="{{URL::asset('/images/gb.png')}}" alt="en" height="14" width="20">
</a>
@else
<a class="lang en" data-lang="en" href="#">
<img src="{{URL::asset('/images/gb.png')}}" alt="en" height="14" width="20">
</a>
@endif
@if (Config::get('app.locale') == 'de')
<a class="lang de active" data-lang="de" href="#">
<img src="{{URL::asset('/images/de.png')}}" alt="de" height="14" width="20">
</a>
@else
<a class="lang de" data-lang="de" href="#">
<img src="{{URL::asset('/images/de.png')}}" alt="de" height="14" width="20">
</a>
@endif
@if (Config::get('app.locale') == 'ru')
<a class="lang ru active" data-lang="ru" href="#">
<img src="{{URL::asset('/images/ru.png')}}" alt="ru" height="14" width="20">
</a>
@else
<a class="lang ru" data-lang="ru" href="#">
<img src="{{URL::asset('/images/ru.png')}}" alt="ru" height="14" width="20">
</a>
@endif
/Middleware/Local.php
<code>
<?php
namespace App\Http\Middleware;
use Closure;
use Session;
use App;
class Locale
{
public function handle($request, Closure $next)
{
$locale = Session::get('locale');
App::setLocale($locale);
return $next($request);
}
}
</code>
web.php
Route::get('/home', 'HomeController@index')->name('home');
Route::prefix('/home')->middleware('locale')->group(function() {
Route::get('/', function () {
return view('home');
});
});
Route::get('/switchLang/{lang}', 'SwitchLanguageController@switchLang')->name('switchLanguage');
Route::get('/home', 'HomeController@index')->name('home');
Route::prefix('/home')->middleware('locale')->group(function() {
Route::get('/', function () {
return view('home');
});
});
Route::get('/switchLang/{lang}', 'SwitchLanguageController@switchLang')->name('switchLanguage');
app.js
$(function(){
var currentLanguage = document.documentElement.lang;
// Switch languages
$('.lang').on('click', function($event) {
$event.preventDefault();
var $selectedLang = $(this).data('lang');
$.ajax({
url: '/switchLang/' + $selectedLang,
type: 'GET',
success: function(response)
{
location.href = window.location.href;
}
});
});
});
SwitchLanguageController.php
$(function(){
var currentLanguage = document.documentElement.lang;
// Switch languages
$('.lang').on('click', function($event) {
$event.preventDefault();
var $selectedLang = $(this).data('lang');
$.ajax({
url: '/switchLang/' + $selectedLang,
type: 'GET',
success: function(response)
{
location.href = window.location.href;
}
});
});
});
答案 0 :(得分:0)
我找到了解决我问题的方法。问题出在我的 web.php 中。
此代码在 Laravel 5.6 上对我不起作用;
Route::get('/users/profile', 'UsersController@profile')->name('profile');
Route::prefix('/users/profile')->middleware('locale')->group(function() {
Route::get('/users/profile', function () {
return view('profile');
});
});
Route::get('/users/profile', 'UsersController@profile')->name('profile');
Route::prefix('/users/profile')->middleware('locale')->group(function() {
Route::get('/users/profile', function () {
return view('profile');
});
});
我用上面的代码替换了上面的代码!
在 Laravel 5.6 上为我工作:
Route::get('/users/profile', 'UsersController@profile')->name('profile')->middleware('locale');
现在,本地化适用于上述每条路线。 :)
非常感谢@Devon的帮助。 Laravel 5.6的文档确实可以帮助我解决此问题。 https://laravel.com/docs/5.6/middleware