Odoo 15中如何通过上下文传递报表语言?设置无效求助
问题解决:Odoo 15报表动态设置语言不生效
核心问题原因
你的报表模板中直接使用t-lang="env.context.get('lang')"是错误写法——QWeb会把这个值当作字符串字面量处理,而非执行Odoo表达式获取上下文里的语言值,因此实际生效的是系统默认语言(西班牙语)。
修复步骤
1. 修改报表模板的动态语言绑定方式
将t-lang替换为t-att-lang,这是QWeb中绑定动态属性值的标准语法,会自动计算表达式结果:
<template id="report_unload_order_template_lang"> <t t-call="web.html_container"> <t t-foreach="docs" t-as="o"> <t t-call="inc_trans_euconsa.report_unload_order_template" t-att-lang="env.context.get('lang')"/> </t> </t> </template>
2. 确认上下文传递有效性
你的向导代码中两种上下文传递方式均有效,无需修改:
report.with_context(lang=self.lang).report_action(self.movement_id)report.with_context({'lang': self.lang}).report_action(self.movement_id)
额外检查点
- 确认系统已安装并启用目标语言(如
en_US)的翻译包,可在设置 > 翻译 > 导入翻译中检查状态 - 验证报表动作
inc_trans_euconsa.action_report_unload_temp关联的模板确实是修改后的report_unload_order_template_lang - 可在模板中临时添加调试代码,确认上下文语言值是否正确传递:
<!-- 在模板开头添加调试代码 --> <span t-esc="env.context.get('lang')"/>
内容的提问来源于stack exchange,提问作者tomasraigal
相关产品推荐
相关产品推荐

