Laravel版本7.6.2
我试着用mcamara/laravel定位包。
https://github.com/mcamara/laravel-localization
我遵循了他们在github页面中给出的指示,并做了一些个性化设置,允许我使用另一个字段,而不是使用id来到达。
这是我的路线:
use Illuminate\Support\Facades\Route;
Route::group(
[
'prefix' => LaravelLocalization::setLocale(),
'middleware' => [ 'localize', 'localeSessionRedirect', 'localizationRedirect', 'localeViewPath' ]
],function () {
Route::get('/', 'HomeController@index')->name('home');
Route::get(LaravelLocalization::transRoute('routes.vehicle'), 'VehicleController@index')->name('vehicle');
Route::get(LaravelLocalization::transRoute('routes.vehicle/{vehicle_url}'), 'VehicleController@show')->name('vehicle.show');
});
车辆工作台结构:
public function up()
{
Schema::create('vehicles', function (Blueprint $table) {
$table->id();
$table->string('vehicle_url')->unique();
$table->string('name_en');
$table->string('name_de');
$table->text('descr_en');
$table->text('descr_de');
$table->timestamps();
});
}
车辆弹丸工作台结构:
public function up()
{
Schema::create('vehicle_slugs', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('vehicle_id');
$table->string('locale',2);
$table->string('slug')->unique();
$table->timestamps();
});
}
这是车辆模型:
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\VehicleSlugs;
class Vehicle extends Model implements \Mcamara\LaravelLocalization\Interfaces\LocalizedUrlRoutable
{
public function getRouteKeyName()
{
return 'vehicle_url';
}
public function slugs()
{
return $this->hasMany(VehicleSlugs::class);
}
public function getLocalizedRouteKey($locale)
{
return $this->slugs->where('locale', '=', $locale)->first()->slug;
}
public function resolveRouteBinding($slug, $field = NULL)
{
return static::whereHas('slugs', function ($q) use ($slug) {
$q->where('slug', '=', $slug);
})->first() ?? abort('404');
}
}
这位是财务主任:
namespace App\Http\Controllers;
use App\Vehicle;
use Illuminate\Http\Request;
class VehicleController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return view('vehicles');
}
/**
* Display the specified resource.
*
* @param \App\Vehicle $vehicle
* @return \Illuminate\Http\Response
*/
public function show(Vehicle $vehicle_url)
{
return view('vehicle', compact('vehicle_url'));
}
这是vehicle.blade.php:
<!DOCTYPE html>
<html lang="{{app()->getLocale()}}">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Testing mcamara</title>
</head>
<body>
<a href="{{route('home')}}">Home</a>
<a href="{{route('vehicle')}}">Vehicle</a>
@forelse(App\Vehicle::all() as $vehicle)
<a href="{{route('vehicle.show', $vehicle->vehicle_url)}}">{{$vehicle->name_en}}</a> <!-- For Example Bikes/Motocycles/Cars/.. -->
@empty
<a href="#">No Link</a>
@endforelse
</body>
</html>
这是用于翻译的数组(/resourse/lang/de/routeres.php)。
return [
"vehicles" => "fahrzeuge",
"vehicle/{vehicle_url}" => "fahrzeug/{vehicle_url}",
]; //The English file is the same with vehicles and vehicle instead of fahrzeuge and farhzeug.
好吧,如果我在地址栏上输入app.loc/de/fahrzeuge/fahrrader,它可以很好地工作,我可以用正确的翻译到达自行车页面,但不能使用视图中的链接翻译app.loc/de/fahrzeuge/{slug}片段。
我肯定错过了什么。有人能帮忙吗?
发布于 2022-02-01 15:23:37
我也遇到了同样的问题,我正在寻找,我给出了这篇文章--它与解决方案这里有关,但是我使用了下面的包星状体和姆卡马拉
很明显,您的翻译路线声明得很糟糕,您必须不使用参数。
Route::get(LaravelLocalization::transRoute('routes.vehicle'), 'VehicleController@show')->name('vehicle.show');
路由文件是用相应的语言解决的。
/resourse/lang/de/Broades.php
这是我在前面的数据包中使用的解决方案。
public function state(State $state)
{
$state = State::whereTranslation('slug', $state->slug)->firstOrFail();
if ($state->translate()->where('slug', $state->slug)->first()->locale != app()->getLocale())
{
return redirect()->route('state.show', $state->translate()->slug);
}
return view('front.states.state', compact('_label', 'state'));
}
https://stackoverflow.com/questions/61397240
复制相似问题