2012-03-07 68 views
6

我有這樣的代碼字:distance_of_time_in_words然後刪除「大約」

= distance_of_time_in_words(Time.now, real_time + 0.seconds, true) 

至極產生也喜歡:

about 15 hours 
less than 

有沒有辦法從刪除單詞「關於」結果如何?已經搜索了很多,但無法找到任何信息,該功能本身就非常棒,它可以將小時,分鐘,秒等很好地返回,但最糟糕的「關於」必須去!任何人都知道?謝謝!

+0

當心 - 刪除「約」字,「低於」,「過度」和「差不多」意味着你的時間戳變得有點少真。如果它是729天之前更新「1年前」?考慮縮短「約」到「〜」,「小於」和「幾乎」到「<」和「超過」到「>」。 – steveluscher 2013-08-28 00:26:08

回答

10

distance_of_time_in_words(Time.now, real_time + 0.seconds, true).gsub('about ','')

here瞭解更多信息

嘗試幫手(放在應用程序/傭工/ application_helper.rb這個代碼)

def remove_unwanted_words string 
    bad_words = ["less than", "about"] 

    bad_words.each do |bad| 
    string.gsub!(bad + " ", '') 
    end 

    return string 
end 
在惡劣的話

你可以定義字符串,你想要從該字符串中刪除。 用戶這樣的:

<%= remove_unwanted_words distance_of_time_in_words(Time.now, real_time + 0.seconds, true) %>

+0

thx我沒有想過只是刪除這個詞,我還需要刪除文本「不到」,你能提供如何刪除多個文本的例子嗎? Thx我還在學習 – Rubytastic 2012-03-07 21:05:28

+0

更新我的答案 – klump 2012-03-07 21:17:41

+0

偉大的大thx!我已將其設置爲已接受的答案 – Rubytastic 2012-03-07 22:33:55

15

由於distance_of_time_in_words使用定位鍵,你可以簡單地覆蓋它們來實現你在的I18n安全的方式想要什麼:

配置/區域設置/ en.yml :

en: 
    # Used in distance_of_time_in_words(), distance_of_time_in_words_to_now(), time_ago_in_words() 
    datetime: 
    distance_in_words: 
     half_a_minute: "half a minute" 
     less_than_x_seconds: 
     one: "1 second" # default was: "less than 1 second" 
     other: "%{count} seconds" # default was: "less than %{count} seconds" 
     x_seconds: 
     one: "1 second" 
     other: "%{count} seconds" 
     less_than_x_minutes: 
     one: "a minute" # default was: "less than a minute" 
     other: "%{count} minutes" # default was: "less than %{count} minutes" 
     x_minutes: 
     one: "1 minute" 
     other: "%{count} minutes" 
     about_x_hours: 
     one: "1 hour" # default was: "about 1 hour" 
     other: "%{count} hours" # default was: "about %{count} hours" 
     x_days: 
     one: "1 day" 
     other: "%{count} days" 
     about_x_months: 
     one: "1 month" # default was: "about 1 month" 
     other: "%{count} months" # default was: "about %{count} months" 
     x_months: 
     one: "1 month" 
     other: "%{count} months" 
     about_x_years: 
     one: "1 year" # default was: "about 1 year" 
     other: "%{count} years" # default was: "about %{count} years" 
     over_x_years: 
     one: "1 year" # default was: "over 1 year" 
     other: "%{count} years" # default was: "over %{count} years" 
     almost_x_years: 
     one: "1 year" # default was: "almost 1 year" 
     other: "%{count} years" # default was: "almost %{count} years" 
+1

您好,先生是傳奇 – edward 2017-05-31 01:36:29

+1

謝謝您提到這一點。 – leenasn 2017-11-14 01:53:46