LoginSignup
4
3

More than 5 years have passed since last update.

i18n-debug の基本的な使い方

Last updated at Posted at 2018-12-28

i18n で参照しているパスを調べてくれる i18n-debug の基本的な使い方が、
ググってもよく分からず、@takumiabe さんに聞いてようやく分かったので、
他の人が同じ調べ物をしなくても済むように上げてみます。

Ruby: 2.5.3
Rails: 5.2.2

やりたいこと

_form.html.haml
= form_with url: user_sessions_path, local: true do |f|
  .field
    = f.label :auth_name
    %br/
    = f.text_field :auth_name
  .field
    = f.label :password
    %br/
    = f.password_field :password
  .actions
    = f.submit 'ログイン'

という、「model を指定しない」簡単なログインフォームを作ろうとしていたが、

config/locales/hoge_ja.yml
ja:
  auth_name: 名前またはメールアドレス
  password: パスワード

と書いても、auth_nameが、
名前またはメールアドレスではなく、
Auth Nameで表示されたりしてしまう。

yml の翻訳のパスが違うために en が出てるんだろうとは思うものの、
どう書くべきかの情報が見つからなかった。

安易な解決策

_form.html.haml
 = label_tag t(:auth_name)

と書けば出たけど、アホくささが拭えない。

参照されている翻訳のパスを調べる

i18n-debugをインストール

i18n-debugで調べられるとのことなので、入れてみる。

Gemfile
group :development do
  ・・・
  gem 'i18n-debug'
end

を追加してbundle install

翻訳参照ログ出力の設定

config/environment/development.rb
I18n::Debug.logger = Logger.new(Rails.root.join('log', 'i18n-debug.log'))

を追加し、Railsサーバー を再起動すると、
(Rails.root)/log/i18n-debug.log
i18n の参照ログが出力されるようになった。

翻訳参照ログを見てみる

ちなみに、こういう稼働中のログを監視する時は

tail -f -n300 log/i18n-debug.log

みたいなのを打つと便利。

log/i18n-debug.log
ja.helpers.label.auth_name => nil
ja.helpers.label.password => nil

と出ていたので、
ja.helpers.label.auth_name等が参照されていたことがわかる。

config/locales/hoge_ja.yml
ja:
  helpers:
    label:
      auth_name: 名前またはメールアドレス
      password: パスワード

と変更したら、意図通りの翻訳が出てくれた。

4
3
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
4
3