LoginSignup
9
9

More than 5 years have passed since last update.

新しく国際化ファイルを追加した時は spring を再起動してやらないと rails console に反映されない

Posted at

国際化について rails console でいろいろ試しててはまったのでメモ。

以下のように、日本語用の国際化ファイルを新しく追加して、 console で確認しようと思ったのだが、

config/application.rb
module Hoge
  class Application < Rails::Application
    ...
    config.i18n.available_locales = [:en, :ja]
  end
end
config/locales/ja.yml
ja:
  datetime:
    distance_in_words:
      about_x_hours:
        one: 約1時間
        other: 約%{count}時間
# $ bin/rails c
Loading development environment (Rails 4.1.6)
> helper.distance_of_time_in_words_to_now 0.25.days.ago
=> "about 6 hours"
> I18n.locale = :ja
=> :ja
> helper.distance_of_time_in_words_to_now 0.25.days.ago
=> "translation missing: ja.datetime.distance_in_words.about_x_hours"

という具合に、翻訳が見つからないよ、と怒られてしまった。
server を立ち上げて view から確認すると、きちんと翻訳が見えるのになぜ??

いろいろ調べた結果、どうやら spring を再起動してやらないと、新しい国際化ファイルが rails console に反映されないらしい。

$ spring stop
# $ bin/rails c
> I18n.locale = :ja
=> :ja
> helper.distance_of_time_in_words_to_now 0.25.day.ago
=> "約6時間"

参考にしたサイト

9
9
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
9
9