多语言情况下如何设置商品属性

请问老师,一个要支持多语言的电商网站应该如何设计商品属性表呢?

我想了两种方式来实现,请问老师哪一种方式更合理,更便于之后的搜索呢?

1. 将商品属性表里面的 namevalue 都设为 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}

这种方式的话,搜索的索引就和教程一样了。但这样的话需要每个语言都一张表。

《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
讨论数量: 1

laravel本地化,可以参考一下

3年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!