2
1

More than 3 years have passed since last update.

[解決] Railsの言語設定エラー 'translation missing: ja.datetime.distance_in_words.x_days ago' 

Last updated at Posted at 2021-02-28

現状確認

ruby 2.6.6
Rails 6.1.0

インスタンス変数は、正しく定義されている。

[1] pry(#<#<Class:0x00007ff69c4e9978>>)> @post.created_at
=> Sun, 31 Jan 2021 08:28:26.434149000 UTC +00:00
[2] pry(#<#<Class:0x00007ff69c4e9978>>)> time_ago_in_words(@post.created_at)
=> "translation missing: ja.datetime.distance_in_words.x_days"

ファイルを見ると、日本語になっている(自分がen→jaに変えたことに気づいていなかった)

application.rb
config.i18n.default_locale = :ja

解決

今までtime_ago_in_wordsは英語設定だったから、上記の日本語変更でエラーが起きたっぽい

time_ago_in_wordsを日本語化するする

locales/ja.ymlにこれを書く


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

ブラウザで確認した、エラーは無くなっていた。

参考

【Rails】time_ago_in_words メソッドで created_atを「〜前」と表示する | RemoNote

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1