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

Flutter Intl本地化:如何配置ARB文件实现整数占位符前导零?

解决Flutter ARB本地化整数补前导零的问题

可以通过调整ARB文件的配置实现需求,以下是正确的配置方式:

正确的ARB片段配置

"notificationScheduled": "The notification has been scheduled for {min}:{sec}",
"@notificationScheduled": {
  "placeholders": {
    "min": {
      "type": "int",
      "format": "decimalPattern",
      "example": "23"
    },
    "sec": {
      "type": "int",
      "format": "custom",
      "example": "08",
      "optionalParameters": {
        "customPattern": "00"
      }
    }
  }
}

配置说明

  • 将sec的format设为custom,对应intl库的NumberFormat自定义格式构造函数
  • 在optionalParameters中指定customPattern为00,表示强制显示两位数字,不足两位时自动补前导零
  • 执行flutter gen-l10n命令不会再报错,传入sec=8时会显示为08

为什么之前的配置不生效

  • 直接将format设为00或0#不符合intl库的格式命名规范,因此触发报错
  • 之前使用decimalPattern配合customPattern的方式无效,因为decimalPattern对应固定的十进制格式化逻辑,不会读取customPattern参数

备选方案(若ARB配置仍有问题)

如果上述配置因环境或版本问题无法生效,可在Dart代码中提前将数字格式化为两位字符串后再传入:

var min = 10;
var sec = 8;
var formattedSec = sec.toString().padLeft(2, '0');

Text(AppLocalizations.of(context)!.notificationScheduled(min, formattedSec))

此时需要将ARB中sec的type改为String:

"sec": {
  "type": "String",
  "example": "08"
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 23:20:26