2017-03-01 66 views

回答

1

如果你看看distance_of_time_in_words方法的文檔,其中time_ago_in_words引擎蓋下使用,你會看到,你可以通過自定義轉換查找一個scope選項:

隨着scope選項,你可以定義Rails的自定義範圍將翻譯爲 。

例如,您可以在您的語言環境中定義以下內容(例如en.yml)。

datetime: 
    distance_in_words: 
    short: 
     about_x_hours: 
     one: 'an hour' 
     other: '%{count} hours' 

見github.com/svenfuchs/rails-i18n/blob/master/rails/locale/en.yml爲 更多的例子。

那麼這將導致以下:

from_time = Time.now distance_of_time_in_words(from_time, from_time + 50.minutes, scope: 'datetime.distance_in_words.short') 
# => "an hour" 
distance_of_time_in_words(from_time, from_time + 3.hours, scope: 'datetime.distance_in_words.short') 
# => "3 hours" 
相關問題