多语言情况下如何设置商品属性
请问老师,一个要支持多语言的电商网站应该如何设计商品属性表呢?
我想了两种方式来实现,请问老师哪一种方式更合理,更便于之后的搜索呢?
1. 将商品属性表里面的 name
和 value
都设为 json
类型的数据,例如#
{
...
name: {
'zh-CN': '产地',
'en-US': 'Origin',
'ja': '原点'
},
value: {
'zh-CN': '北京',
'en-US': 'Beijing',
'ja': '北京'
}
}
app/Models/ProductProperty.php
protected $casts = [
'name' => 'json',
'value' => 'json'
];
public function getNameAttribute(){
$locale = app()->getLocale();
return $this->name->{'$locale'};
}
public function getValueAttribute(){
$locale = app()->getLocale();
return $this->value->{'$locale'};
}
通过一个前置中间件,依据用户的请求头来设置 locale
:
public function handle($request, Closure $next){
$locale = $request->hasHeader('Locale') ? $request->header('Locale') : config('app.locale');
app()->setLocale($locale);
return $next($request);
}
但是如果这样做的话,之后的搜索应该如何建立索引呢?
2. 每种语言单独设立一个 product_properties_{locale}
表#
这种方式的话,搜索的索引就和教程一样了。但这样的话需要每个语言都一张表。
推荐文章: