OpenHarmony图表国际化支持
一 总体思路
二 资源与文本的国际化
三 数字日期与单位的本地化
四 与图表库集成示例 MPChart
资源 zh/element/string.json: { “name”: “unit_sales”, “value”: “销售额(元)” } en/element/string.json: { “name”: “unit_sales”, “value”: “Sales (CNY)” }
数据与格式化 import i18n from ‘@ohos.i18n’; import intl from ‘@ohos.intl’; import { BarChart, BarDataSet, BarEntry, IBarDataSet } from ‘@ohos/mpchart’;
// 1) 读取当前语言,用于数字/单位本地化 const langTag = i18n.getSystemLanguage(); // 如 zh-Hans-CN / en-US const numFmt = new intl.NumberFormat(langTag, { style: ‘decimal’, minimumFractionDigits: 0, maximumFractionDigits: 2 }); const unit = $r(‘app.string.unit_sales’);
// 2) 构造数据(示例) const entries: BarEntry[] = [ new BarEntry(0, 1234567.89), new BarEntry(1, 987654.32) ];
// 3) 图例与轴标签本地化 const set = new BarDataSet(entries, $r(‘app.string.company_a’)); // 图例项来自资源 const data = new BarData([set]);
// 4) 轴格式化:千分位 + 单位
leftAxis.setValueFormatter((value: number) => ${numFmt.format(value)} ${unit});
// 5) 绑定到图表
model.setData(data);
上述示例展示了在将数据送入 MPChart 前完成文本与数字本地化的关键步骤;图例名、单位、轴标签均来自资源或 Intl 格式化结果,从而在不同语言环境下正确显示。
五 进阶与注意事项
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。