Shopware 6:插件中en-GB.json自定义翻译无法覆盖默认片段
Shopware 6插件自定义翻译无法覆盖默认片段的问题解决
问题概述
开发Shopware 6插件时,已在en-GB.json中定义自定义翻译,但系统仍显示默认值。预期labelBoundSalesChannel显示“Bound Sales Channel”、emptyBoundSalesChannel显示“None”,实际显示“Sales Channel”和“All”。
环境信息:
- Shopware版本:6.6.4.1
- PHP版本:8.2
相关代码
自定义翻译文件(en-GB.json)
{ "sw-customer": { "baseInfo": { "labelBoundSalesChannel": "Bound Sales Channel", "emptyBoundSalesChannel": "None" } } }
Twig模板(sw-customer-base-info)
{% block sw_customer_base_metadata_bound_sales_channel %} <sw-description-list> {% block sw_customer_base_metadata_bound_sales_channel_label %} <dt class="sw-customer-base-info__label sw-bound-sales-channel__label"> {{ $tc('sw-customer.baseInfo.labelBoundSalesChannel') }} <sw-help-text :text="$tc('sw-customer.baseInfo.helpTextBoundSalesChannel')" /> </dt> {% endblock %} {% block sw_customer_base_metadata_bound_saleschannel_content %} <dd class="sw-customer-base__label-bound-sales-channel"> <template v-if="customer.boundSalesChannelId"> {{ customer.salesChannel.translated.name }} </template> <template v-else> {{ $tc('sw-customer.baseInfo.emptyBoundSalesChannel') }} </template> </dd> {% endblock %} </sw-description-list> {% endblock %}
排查与解决步骤
- 检查翻译文件路径:确保文件放在插件的
src/Resources/app/administration/src/snippet/en-GB/en-GB.json路径下,路径错误会导致系统无法加载自定义翻译。 - 注册snippet资源:在插件的
src/Resources/app/administration/src/main.js中导入翻译文件:
若使用Shopware 6.4+新结构,需确保import './snippet/en-GB/en-GB.json';package.json或webpack.config.js中正确配置了snippet的加载规则。 - 清除缓存并重构前端:
执行缓存清理命令:
本地开发环境重构前端资源:bin/console cache:clear
生产环境编译主题:./psh.phar administration:buildbin/console theme:compile - 校验翻译键名:确认Twig中使用的翻译键(如
sw-customer.baseInfo.labelBoundSalesChannel)与JSON文件中的键名完全一致,Shopware翻译键区分大小写和层级结构。 - 检查插件加载优先级:若有其他插件或主题定义了相同翻译键,需确保你的插件加载顺序在它们之后。可在插件的
composer.json中通过extra.shopware.plugin.dependencies配置依赖关系调整加载顺序。 - 确认后台语言设置:检查当前后台使用的语言是否为
en-GB,可切换语言后再测试翻译是否生效。
内容的提问来源于stack exchange,提问作者Auhona
相关产品推荐
相关产品推荐

