[Ruby on Rails 3, Ruby on Rails 4] time_ago_in_words ヘルパーメソッド

例えば Micropost の投稿時間を「○○時間前」のようにしたい場合。
日本語表記にする。

[markdown]
> * [10.2.1ユーザー表示ページの拡張](http://railstutorial-ja.herokuapp.com/chapters/user-microposts#sec-augmenting_the_user_show_page)

下記のような表示になったので調べてみた。

“`prettyprinted
Posted translation missing: ja.datetime.distance_in_words.about_x_hours ago.
“`

### time_ago_in_words

> * [time_ago_in_words(from_time, include_seconds_or_options = {})](http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-time_ago_in_words)

### config/locales/ja.yml

config/locales/ja.yml になかったからのようなので、設定してみる。

> * [time_ago_in_wordsでtwitterライクな「○分前」を表示 – mochizblog](http://mochizblog.heroku.com/28)
> * [ruby – Rails distance_of_time_in_words returns "en, about_x_hours" – Stack Overflow](http://stackoverflow.com/questions/4007073/rails-distance-of-time-in-words-returns-en-about-x-hours)

“`
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}分”
about_x_hours:
one: “約1時間”
other: “約%{count}時間”
“`

こちらを使ってみると良いのだと思う。

> * [rails-i18n/rails/locale/ja.yml at master · svenfuchs/rails-i18n](https://github.com/svenfuchs/rails-i18n/blob/master/rails/locale/ja.yml)

### 変更結果

“`prettyprinted
Posted 約2時間 ago.
“`

前後の英語は、以下のパーシャルを直す。

“`prettyprinted
app/views/microposts/_micropost.html.erb
“`
[/markdown]