🖥

#Rails の ActionView::Helpers を直接 include して i18n チックに xヶ月 x日 x年 みたいなのを

2019/08/27に公開

#Rails' ActionView :: Helpers is included directly to display i18n ticks like x month x day x year

rails console で動作確認した

ja.yml

なぜかトップレベルに設定する必要がありそう?

ja:
  about_x_hours:
    one: 約1時間
    other: 約%{count}時間
  about_x_months:
    one: 約1ヶ月
    other: 約%{count}ヶ月
  about_x_years:
    one: 約1年
    other: 約%{count}almost_x_years:
    one: 1年弱
    other: "%{count}年弱"
  half_a_minute: 30秒前後
  less_than_x_seconds:
    one: 1秒以内
    other: "%{count}秒未満"
  less_than_x_minutes:
    one: 1分以内
    other: "%{count}分未満"
  over_x_years:
    one: 1年以上
    other: "%{count}年以上"
  x_seconds:
    one: 1秒
    other: "%{count}秒"
  x_minutes:
    one: 1分
    other: "%{count}分"
  x_days:
    one: 1日
    other: "%{count}日"
  x_months:
    one: 1ヶ月
    other: "%{count}ヶ月"
  x_years:
    one: 1年
    other: "%{count}年"

rails console

require 'action_view'
require 'action_view/helpers'

include ActionView::Helpers

distance_of_time_in_words(Time.zone.now, 3.months.after)
# => "3ヶ月"

Ref

https://github.com/svenfuchs/rails-i18n/blob/master/rails/locale/ja.yml

ja.yml 設定例

https://edgeapi.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html

Original by Github issue

https://github.com/YumaInaura/YumaInaura/issues/2355

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

https://line.me/ti/g2/eEPltQ6Tzh3pYAZV8JXKZqc7PJ6L0rpm573dcQ

Twitter

https://twitter.com/YumaInaura

公開日時

2019-08-27

Discussion