【问题标题】:TCG\Voyager doesn't get translation on relationshipTCG\Voyager 没有得到关于关系的翻译
【发布时间】:2021-12-22 22:18:12
【问题描述】:

我将 Voyager 管理界面 (v.1.4) 用于 Laravel (v8.0)。 Voyager 支持多种语言(官方文档:https://voyager-docs.devdojo.com/v/1.4-1/core-concepts/multilanguage)。

我有这种关系:

  • 进程属于多台工作机
  • 流程有很多产品

流程模型:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use TCG\Voyager\Traits\Translatable;

class Process extends Model
{
    use Translatable;

    protected $translatable = ['name', 'meta_description', 'description'];

    public function get_workmachine() {
        return $this->belongsToMany(WorkMachine::class, 'process_workmachine');
    }

    public function get_products() {
        return $this->hasMany(Product::class, 'process_product');
    }

WorkMachine 模型:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use TCG\Voyager\Traits\Translatable;

class WorkMachine extends Model
{
    use Translatable;

    protected $translatable = ['name', 'meta_description', 'description'];

产品型号:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use TCG\Voyager\Traits\Translatable;

class Product extends Model
{
    use Translatable;

    protected $translatable = ['name'];

在我的控制器中:

$process = App\Models\Process::where('slug', $processSlug)
                    ->with('get_workmachine')
                    ->with('get_products')
                    ->firstOrFail()->translate(app()->getLocale());

问题是:处理可翻译文本有效,输出语言取决于app()-&gt;getLocale()。但是工作机文本和产品文本没有翻译。

我也尝试使用:

->with(['get_workmachine' => function ($query) { $query->withTranslation('de'); }])

但它没有被翻译。

有什么办法可以翻译这种关系吗?

【问题讨论】:

    标签: php laravel relationship translate voyager


    【解决方案1】:

    我找到了一个可能的解决方案! 在我的 .blade 文件中,而不是:

    @foreach(json_decode($process->get_workmachine) as $workmachine)
        ...
        ...
    @endforeach
    

    我加了-&gt;translate(app()-&gt;getLocale())

    @foreach(json_decode($process->get_workmachine->translate(app()->getLocale())) as $workmachine)
        ...
        ...
    @endforeach
    

    这行得通!

    【讨论】:

      猜你喜欢
      • 2019-10-24
      • 2020-01-28
      • 2023-03-04
      • 2020-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-27
      • 2019-05-06
      相关资源
      最近更新 更多