如何在DevExpress Angular报表中本地化表名称与列名称
DevExpress Angular报表本地化表名称与列名称解决方案
针对您使用的Platform: Angular Reporting、Product: Reporting for Angular、Framework:.NET Core、Build:21.2.14环境,以下是本地化报表表名称与列名称的具体实现步骤:
一、后端数据源字段本地化(绑定实体类场景)
如果报表绑定的是实体类数据源,可通过数据注解+资源文件实现字段名称本地化:
- 实体类中为字段添加
DisplayAttribute,指定本地化资源键和资源类型:using System.ComponentModel.DataAnnotations; public class Order { [Display(Name = "OrderID_Local", ResourceType = typeof(ReportResources))] public int OrderID { get; set; } [Display(Name = "CustomerName_Local", ResourceType = typeof(ReportResources))] public string CustomerName { get; set; } } - 创建多语言资源文件(如
ReportResources.resx默认语言、ReportResources.zh-CN.resx中文),添加对应键值对:ReportResources.zh-CN.resx示例:
OrderID_Local = "订单ID"
CustomerName_Local = "客户名称" - 配置后端本地化中间件(Program.cs/Startup.cs):
var supportedCultures = new[] { "en-US", "zh-CN" }; var localizationOptions = new RequestLocalizationOptions() .SetDefaultCulture("zh-CN") .AddSupportedCultures(supportedCultures) .AddSupportedUICultures(supportedCultures); app.UseRequestLocalization(localizationOptions);
二、报表模板直接绑定本地化资源
若报表是手动设计的表控件,可直接在设计器中绑定本地化文本:
- 选中表控件,在属性面板找到
Caption属性,点击绑定按钮选择表达式,输入:Localizer.GetString("TableName_Local") - 选中列标题单元格,在
Text属性中使用表达式绑定对应资源键:
(注:Localizer.GetString("ColumnName_Local")TableName_Local和ColumnName_Local需在后端资源文件中定义对应多语言值)
三、前端Angular项目本地化配置
- 安装对应版本的DevExpress本地化包:
npm install devexpress-localization@21.2.14 - 在
main.ts中引入中文本地化脚本:import 'devexpress-localization/localization/dx.all.zh'; - 在报表组件中设置当前语言:
import { Component } from '@angular/core'; import { DevExpress } from 'devexpress-reporting-angular'; @Component({ selector: 'app-report', templateUrl: './report.component.html' }) export class ReportComponent { constructor() { DevExpress.localization.locale('zh-CN'); } }
四、动态切换语言(可选)
若需要支持前端切换语言,可在后端报表控制器中设置线程文化:
using System.Globalization; using System.Threading; using Microsoft.AspNetCore.Mvc; using DevExpress.Web.Mvc; public class ReportController : ReportController { protected override void OnActionExecuting(ActionExecutingContext context) { var culture = context.HttpContext.Request.Query["culture"]; if (!string.IsNullOrEmpty(culture)) { Thread.CurrentThread.CurrentCulture = new CultureInfo(culture); Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture); } base.OnActionExecuting(context); } }
内容的提问来源于stack exchange,提问作者Amir133
相关产品推荐
相关产品推荐

