
在使用laravel框架配合voyager管理后台开发多语言应用时,我们通常会利用voyager提供的tcg\voyager\traits\translatable trait来实现模型的字段翻译。当主模型(例如process)的自身可翻译字段(如name、description)能够根据当前应用语言环境正确显示时,其通过关系(如belongstomany或hasmany)加载的关联模型(例如workmachine、product)的可翻译字段却可能无法同步进行翻译,即便这些关联模型也正确使用了translatable trait。这导致用户在切换语言时,关联数据仍以默认语言显示,影响了多语言体验的完整性。
为了实现多语言功能,所有需要翻译的Eloquent模型都必须引入Translatable trait,并定义$translatable属性,列出所有需要翻译的字段。
示例模型定义:
// app/Models/Process.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');
}
}
// app/Models/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'];
}
// app/Models/Product.php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use TCG\Voyager\Traits\Translatable;
class Product extends Model
{
use Translatable;
protected $translatable = ['name'];
}在控制器中,我们通常会这样加载主模型及其关联模型,并尝试对其进行翻译:
// 在控制器中
use App\Models\Process;
use Illuminate\Support\Facades\App;
$processSlug = 'some-process-slug';
$process = Process::where('slug', $processSlug)
->with('get_workmachine')
->with('get_products')
->firstOrFail()
->translate(App::getLocale()); // 对主模型进行翻译上述代码能够确保$process模型自身的name、meta_description、description等字段根据当前语言环境进行翻译。然而,$process-youjiankuohaophpcnget_workmachine和$process->get_products所代表的关联模型集合中的字段却可能保持未翻译状态。
为了解决关联模型的翻译问题,开发者可能会尝试在with()方法中利用闭包对关联查询应用翻译作用域,例如:
// 尝试在with()中应用翻译(可能无效)
$process = Process::where('slug', $processSlug)
->with(['get_workmachine' => function ($query) {
$query->withTranslation(App::getLocale()); // 尝试对关联模型应用翻译
}])
->with('get_products') // 假设这里也尝试了类似操作
->firstOrFail()
->translate(App::getLocale());尽管withTranslation()方法是Translatable trait提供的一个查询作用域,旨在加载特定语言的翻译,但在某些特定场景或版本组合下,这种直接在with闭包中应用翻译作用域的方式可能不会按预期工作,导致关联模型仍然未被翻译。这可能是由于Eloquent加载机制、trait的内部实现细节或缓存行为等因素造成的。
最直接且可靠的解决方案是,在访问关联模型集合时,显式地对该集合中的每个模型应用translate()方法。这通常在Blade视图文件中进行,因为这是我们最终渲染数据的地方。
当translate()方法被调用在一个Eloquent模型集合上时,它会自动遍历集合中的每个模型实例,并对每个实现了Translatable trait的模型应用翻译逻辑。
示例:在Blade视图中应用翻译
假设你的Blade视图需要遍历Process的关联工作机器:
{{-- 假设 $process 变量已从控制器传递过来 --}}
<h2>{{ $process->name }}</h2>
<p>{{ $process->description }}</p>
<h3>关联工作机器 (Work Machines)</h3>
<ul>
@foreach($process->get_workmachine->translate(app()->getLocale()) as $workmachine)
{{-- 对整个集合调用 translate() 方法 --}}
<li>{{ $workmachine->name }} - {{ $workmachine->meta_description }}</li>
@endforeach
</ul>
<h3>关联产品 (Products)</h3>
<ul>
@foreach($process->get_products->translate(app()->getLocale()) as $product)
{{-- 对整个集合调用 translate() 方法 --}}
<li>{{ $product->name }}</li>
@endforeach
</ul>通过在@foreach循环之前,对$process->get_workmachine和$process->get_products这两个Eloquent集合调用->translate(app()->getLocale()),我们确保了在迭代每个关联模型时,其可翻译字段都已根据当前应用语言环境进行了转换。
TCG\Voyager\Traits\Translatable trait为Eloquent模型添加了translate()方法。当这个方法在一个Illuminate\Database\Eloquent\Collection实例上被调用时,Laravel的集合特性会确保该方法作用于集合中的每个元素。如果集合中的元素是实现了Translatable trait的Eloquent模型,那么每个模型实例的translate()方法就会被执行,从而实现字段的翻译。
这种方法的好处在于:
翻译时机: 虽然在视图层进行翻译简单有效,但如果你的业务逻辑需要在控制器或服务层就获取到已翻译的关联数据,你也可以在那里进行处理:
// 在控制器中预先翻译关联模型
$process = Process::where('slug', $processSlug)
->with(['get_workmachine', 'get_products'])
->firstOrFail();
$currentLocale = App::getLocale();
$process->translate($currentLocale); // 翻译主模型
// 遍历关联集合并翻译每个模型
$process->get_workmachine->each(fn($workmachine) => $workmachine->translate($currentLocale));
$process->get_products->each(fn($product) => $product->translate($currentLocale));
// 现在 $process 及其所有关联模型都已翻译,可以直接传递给视图
return view('your.view', compact('process'));这种方式可以确保在视图层获取到的数据已经是完全翻译过的,减少视图逻辑的复杂性。
性能考虑: 对于大型集合,each()循环可能会带来轻微的性能开销。但在大多数Web应用场景下,这种开销通常可以忽略不计。如果遇到极端性能瓶颈,可能需要考虑更底层的数据库查询优化,例如在加载关联时直接通过SQL获取特定语言的翻译字段(但这会增加复杂性并可能绕过Translatable trait的设计)。
json_decode的误用: 在原始问题中,用户曾尝试对$process->get_workmachine进行json_decode。需要明确的是,belongsToMany和hasMany关系返回的是Eloquent模型集合,而不是JSON字符串。对Eloquent集合直接进行json_decode通常是错误操作,会导致数据解析失败。正确的做法是直接操作Eloquent集合或其中的模型实例。本文提供的解决方案已纠正此潜在误用。
确保Trait正确导入和配置: 再次检查所有涉及多语言的模型是否都正确导入了TCG\Voyager\Traits\Translatable trait,并且$translatable属性中列出了所有需要翻译的字段。
在Laravel和Voyager的多语言应用中,解决关联模型翻译失效问题的关键在于理解Translatable trait的工作机制,并确保translate()方法在正确的时间点作用于正确的对象(即Eloquent模型实例或模型集合)。通过在视图层或控制器中显式地对关联模型集合调用->translate(app()->getLocale()),我们可以有效地确保所有相关内容都能根据当前语言环境进行准确显示,从而提供无缝的多语言用户体验。
以上就是解决Voyager/Laravel中关联模型多语言翻译失效问题的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号