You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何指定语言调用Store API?解决购物车接口英文报错问题

解决Shopware中/checkout/cart/*接口错误信息强制英文的问题

问题根源

你判断的没错,Shopware核心里部分购物车相关的错误提示确实是硬编码实现的,没有接入系统的snippet翻译机制,所以常规的sw-language-id请求头、销售渠道语言配置、snippet修改都不会生效。要解决这个问题,通过自定义插件装饰对应的核心服务是可行的方案。

实现步骤

1. 创建基础插件结构

先建立标准的Shopware插件目录,比如命名为CustomCartErrorTranslation,核心目录结构如下:

CustomCartErrorTranslation/
├── src/
│   ├── Core/
│   │   └── Checkout/
│   │       └── Cart/
│   │           └── CartValidatorDecorator.php
│   ├── Resources/
│   │   ├── config/
│   │   │   └── services.xml
│   │   └── snippet/
│   │       └── de_DE/
│   │           └── cart-error.de-DE.json
│   └── CustomCartErrorTranslation.php
└── composer.json

2. 编写装饰器类替换硬编码错误

先定位抛出硬编码错误的核心类——可以直接搜索核心代码里的英文错误文本(比如"Cart is empty"),通常会定位到\Shopware\Core\Checkout\Cart\CartValidator。我们创建装饰器类重写它的验证逻辑,替换错误信息:

<?php

namespace CustomCartErrorTranslation\Core\Checkout\Cart;

use Shopware\Core\Checkout\Cart\Cart;
use Shopware\Core\Checkout\Cart\CartValidatorInterface;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\Validation\BuildValidationEvent;
use Shopware\Core\System\Snippet\SnippetService;

class CartValidatorDecorator implements CartValidatorInterface
{
    private CartValidatorInterface $originalValidator;
    private SnippetService $snippetService;

    public function __construct(CartValidatorInterface $originalValidator, SnippetService $snippetService)
    {
        $this->originalValidator = $originalValidator;
        $this->snippetService = $snippetService;
    }

    public function validate(Cart $cart, BuildValidationEvent $validationEvent, Context $context): void
    {
        // 执行原验证逻辑
        $this->originalValidator->validate($cart, $validationEvent, $context);
        
        // 获取当前请求的语言ID
        $languageId = $context->getLanguageId();
        
        // 遍历验证错误,替换硬编码文本
        $errors = $validationEvent->getViolations();
        foreach ($errors as $error) {
            $message = $error->getMessage();
            
            // 匹配原硬编码文本与对应的snippet键
            $snippetKey = match ($message) {
                'Cart is empty' => 'cart.error.empty',
                'Minimum order value not reached' => 'cart.error.minOrderValue',
                // 按需添加所有需要替换的硬编码错误文本
                default => null
            };
            
            if ($snippetKey) {
                // 从snippet中获取对应语言的翻译内容
                $translatedMessage = $this->snippetService->get($snippetKey, $languageId);
                if ($translatedMessage) {
                    $error->setMessage($translatedMessage);
                }
            }
        }
    }
}

3. 注册装饰器服务

在src/Resources/config/services.xml中配置装饰器,让系统优先使用我们的装饰类:

<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

    <services>
        <service id="CustomCartErrorTranslation\Core\Checkout\Cart\CartValidatorDecorator"
                 decorates="Shopware\Core\Checkout\Cart\CartValidator"
                 decoration-priority="50">
            <argument type="service" id="CustomCartErrorTranslation\Core\Checkout\Cart\CartValidatorDecorator.inner"/>
            <argument type="service" id="Shopware\Core\System\Snippet\SnippetService"/>
        </service>
    </services>
</container>

4. 添加德语snippet配置

在src/Resources/snippet/de_DE/cart-error.de-DE.json中添加对应翻译:

{
    "cart": {
        "error": {
            "empty": "Warenkorb ist leer",
            "minOrderValue": "Mindestbestellwert nicht erreicht"
        }
    }
}

5. 安装并测试

  • 将插件放入Shopware的custom/plugins目录
  • 执行命令安装并激活插件:bin/console plugin:install CustomCartErrorTranslation -a
  • 清除缓存:bin/console cache:clear
  • 调用/checkout/cart/*接口触发错误,检查返回的错误信息是否已切换为德语

注意事项

  • 如果硬编码错误来自多个核心类(比如CartItemValidator、CartProcessor等),需要为每个类创建对应的装饰器
  • 替换错误文本时要确保原英文文本完全匹配,避免遗漏
  • 可通过bin/console debug:container命令确认核心服务的ID是否正确

内容的提问来源于stack exchange,提问作者Stepan Lukachyna

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 12:22:41