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

I18n缩写月份返回空白问题排查求助

Rails I18n 法语缩写月份显示空白问题

Rails项目中使用I18n渲染法语缩写月份时返回空白,测试用例报错:

Failure:
MessageDecoratorTest#test_it_will_show_date_without_year_if_it's_same_year [/opt/vanadium-web/test/decorators/message_decorator_test.rb:27]:
For locale fr, expected 9 mai, got 9 .
Expected: "9 mai"
  Actual: "9 "

当前fr.yml的日期配置如下:

date:
  abbr_month_names:

    Jan: janv
    Feb: févr.
    Mar: mars
    Apr: avril
    May: mai
    Jun: juin
    Jul: juill.
    Aug: août
    Sep: sept.
    Oct: oct.
    Nov: nov.
    Dec: déc.

问题原因及解决方法

Rails的I18n系统中,abbr_month_names要求使用索引数组形式,而非键值对。数组索引从0开始(索引0留空,对应无月份的场景),1到12依次对应1月到12月。原配置用Jan、Feb等作为键,Rails无法匹配到对应月份的索引,导致渲染时返回空白。

修改后的正确配置:

date:
  abbr_month_names:
    - ""
    - "janv"
    - "févr."
    - "mars"
    - "avril"
    - "mai"
    - "juin"
    - "juill."
    - "août"
    - "sept."
    - "oct."
    - "nov."
    - "déc."

修改后重新运行测试,即可正确渲染出“9 mai”。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 09:22:42