Help us understand the problem. What is going on with this article?

Railsのi18nで定義している辞書情報を軒並み取得する方法

More than 3 years have passed since last update.

以前ど忘れして号泣したのでメモ。

日本語訳ファイルを定義して、

config/locales/ja.yml
ja:
  date:
    formats:
      default: "%Y/%m/%d"
      short: "%m/%d"
      long: "%Y年%m月%d日(%a)"

    day_names: [日曜日, 月曜日, 火曜日, 水曜日, 木曜日, 金曜日, 土曜日]
    abbr_day_names: [, , , , , , ]

    month_names: [~, 1月, 2月, 3月, 4月, 5月, 6月, 7月, 8月, 9月, 10月, 11月, 12月]
    abbr_month_names: [~, 1月, 2月, 3月, 4月, 5月, 6月, 7月, 8月, 9月, 10月, 11月, 12月]

    order:
      - :year
      - :month
      - :day

  time:
    formats:
      default: "%Y/%m/%d %H:%M:%S"
      short: "%y/%m/%d %H:%M"
      long: "%Y年%m月%d日(%a) %H時%M分%S秒 %Z"
    am: "午前"
    pm: "午後"

  number:
    currency:
      format:
        unit: '¥'
        delimiter: ','
        separator: '.'
        precision: 0

  hello: 'world'
  good:
    morning: 'yeah!'

rails consoleでI18n.t('.')と叩いてみる。
hashで取得されるのでこれを必要に応じてあれこれごにょごにょしたりする。

$rails c
$pry(main)> I18n.t('.')
=> {:date=>
  {:formats=>{:default=>"%Y/%m/%d", :short=>"%m/%d", :long=>"%Y年%m月%d日(%a)"},
   :day_names=>["日曜日", "月曜日", "火曜日", "水曜日", "木曜日", "金曜日", "土曜日"],
   :abbr_day_names=>["日", "月", "火", "水", "木", "金", "土"],
   :month_names=>[nil, "1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"],
   :abbr_month_names=>[nil, "1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"],
   :order=>[:year, :month, :day]},
 :time=>{:formats=>{:default=>"%Y/%m/%d %H:%M:%S", :short=>"%y/%m/%d %H:%M", :long=>"%Y年%m月%d日(%a) %H時%M分%S秒 %Z"}, :am=>"午前", :pm=>"午後"},
 :number=>{:currency=>{:format=>{:unit=>"¥", :delimiter=>",", :separator=>".", :precision=>0}}},
 :hello=>"world",
 :good=>{:morning=>"yeah!"}}

shitake
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした