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

Flutter中能否单独创建.arb文件用于格式化配置?

可以单独创建格式化专用的.arb文件

完全没问题,你可以专门创建app_formatting.arb来存放所有格式化配置,这样能让翻译内容和格式化规则彻底分离,结构更规整。具体操作步骤如下:

  • 创建独立的格式化arb文件
    新建app_formatting.arb文件,把所有像你示例里的格式化配置条目移进去,比如:

    "date_format_year_month_day": "{date}",
    "@date_format_year_month_day": {
        "description": "Do not translate",
        "placeholders": {
            "date": {
                "type": "DateTime",
                "format": "yMMMMd",
                "example": "July 10, 1996"
            }
        }
    }
    

    因为这些是格式化规则,不需要翻译,所以不用为这个文件创建其他语言版本。

  • 修改l10n.yaml配置
    在项目的l10n.yaml文件中,通过includes字段把这个新的arb文件纳入本地化生成流程,示例配置如下:

    arb-dir: lib/l10n
    template-arb-file: app_en.arb
    output-localization-file: app_localizations.dart
    includes:
      - app_formatting.arb
    
  • 重新生成本地化代码
    运行flutter gen-l10n命令,Flutter会自动把两个arb文件的内容合并到生成的AppLocalizations类中。

  • 在代码中使用格式化配置
    和调用普通本地化字符串一样,直接通过生成的类调用格式化方法:

    AppLocalizations.of(context)!.dateFormatYearMonthDay(DateTime.now())
    

这样做的好处很明显:翻译类arb文件只保留需要多语言转换的内容,格式化规则单独维护,后续修改或新增格式化配置时更清晰,也不会干扰翻译流程。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 09:07:12