首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Laravel mcamara/laravel-本地化包通行证查看

Laravel mcamara/laravel-本地化包通行证查看
EN

Stack Overflow用户
提问于 2020-04-23 21:24:11
回答 1查看 1.2K关注 0票数 1

Laravel版本7.6.2

我试着用mcamara/laravel定位包。

https://github.com/mcamara/laravel-localization

我遵循了他们在github页面中给出的指示,并做了一些个性化设置,允许我使用另一个字段,而不是使用id来到达。

这是我的路线:

代码语言:javascript
运行
复制
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');

});

车辆工作台结构:

代码语言:javascript
运行
复制
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();
    });
}

车辆弹丸工作台结构:

代码语言:javascript
运行
复制
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();
    });
}

这是车辆模型:

代码语言:javascript
运行
复制
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');
    }
}

这位是财务主任:

代码语言:javascript
运行
复制
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:

代码语言:javascript
运行
复制
<!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)。

代码语言:javascript
运行
复制
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}片段。

我肯定错过了什么。有人能帮忙吗?

EN

回答 1

Stack Overflow用户

发布于 2022-02-01 15:23:37

我也遇到了同样的问题,我正在寻找,我给出了这篇文章--它与解决方案这里有关,但是我使用了下面的包星状体姆卡马拉

很明显,您的翻译路线声明得很糟糕,您必须不使用参数。

代码语言:javascript
运行
复制
Route::get(LaravelLocalization::transRoute('routes.vehicle'), 'VehicleController@show')->name('vehicle.show');

路由文件是用相应的语言解决的。

/resourse/lang/de/Broades.php

这是我在前面的数据包中使用的解决方案。

代码语言:javascript
运行
复制
    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'));
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61397240

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档