Add support for translating time intervals

This commit is contained in:
Omar Roth 2019-02-20 08:49:39 -06:00
parent fb14d9c134
commit 83493237a5
4 changed files with 25 additions and 28 deletions

View File

@ -2558,7 +2558,7 @@ get "/api/v1/videos/:id" do |env|
json.field "description", description json.field "description", description
json.field "descriptionHtml", video.description json.field "descriptionHtml", video.description
json.field "published", video.published.to_unix json.field "published", video.published.to_unix
json.field "publishedText", translate(locale, "`x` ago", recode_date(video.published)) json.field "publishedText", translate(locale, "`x` ago", recode_date(video.published, locale))
json.field "keywords", video.keywords json.field "keywords", video.keywords
json.field "viewCount", video.views json.field "viewCount", video.views
@ -2766,7 +2766,7 @@ get "/api/v1/trending" do |env|
json.field "authorUrl", "/channel/#{video.ucid}" json.field "authorUrl", "/channel/#{video.ucid}"
json.field "published", video.published.to_unix json.field "published", video.published.to_unix
json.field "publishedText", translate(locale, "`x` ago", recode_date(video.published)) json.field "publishedText", translate(locale, "`x` ago", recode_date(video.published, locale))
json.field "description", video.description json.field "description", video.description
json.field "descriptionHtml", video.description_html json.field "descriptionHtml", video.description_html
json.field "liveNow", video.live_now json.field "liveNow", video.live_now
@ -2805,7 +2805,7 @@ get "/api/v1/popular" do |env|
json.field "authorId", video.ucid json.field "authorId", video.ucid
json.field "authorUrl", "/channel/#{video.ucid}" json.field "authorUrl", "/channel/#{video.ucid}"
json.field "published", video.published.to_unix json.field "published", video.published.to_unix
json.field "publishedText", translate(locale, "`x` ago", recode_date(video.published)) json.field "publishedText", translate(locale, "`x` ago", recode_date(video.published, locale))
end end
end end
end end
@ -2840,7 +2840,7 @@ get "/api/v1/top" do |env|
json.field "authorId", video.ucid json.field "authorId", video.ucid
json.field "authorUrl", "/channel/#{video.ucid}" json.field "authorUrl", "/channel/#{video.ucid}"
json.field "published", video.published.to_unix json.field "published", video.published.to_unix
json.field "publishedText", translate(locale, "`x` ago", recode_date(video.published)) json.field "publishedText", translate(locale, "`x` ago", recode_date(video.published, locale))
description = video.description.gsub("<br>", "\n") description = video.description.gsub("<br>", "\n")
description = description.gsub("<br/>", "\n") description = description.gsub("<br/>", "\n")
@ -3015,7 +3015,7 @@ get "/api/v1/channels/:ucid" do |env|
json.field "viewCount", video.views json.field "viewCount", video.views
json.field "published", video.published.to_unix json.field "published", video.published.to_unix
json.field "publishedText", translate(locale, "`x` ago", recode_date(video.published)) json.field "publishedText", translate(locale, "`x` ago", recode_date(video.published, locale))
json.field "lengthSeconds", video.length_seconds json.field "lengthSeconds", video.length_seconds
json.field "liveNow", video.live_now json.field "liveNow", video.live_now
json.field "paid", video.paid json.field "paid", video.paid
@ -3113,7 +3113,7 @@ end
json.field "viewCount", video.views json.field "viewCount", video.views
json.field "published", video.published.to_unix json.field "published", video.published.to_unix
json.field "publishedText", translate(locale, "`x` ago", recode_date(video.published)) json.field "publishedText", translate(locale, "`x` ago", recode_date(video.published, locale))
json.field "lengthSeconds", video.length_seconds json.field "lengthSeconds", video.length_seconds
json.field "liveNow", video.live_now json.field "liveNow", video.live_now
json.field "paid", video.paid json.field "paid", video.paid
@ -3133,6 +3133,8 @@ end
["/api/v1/channels/:ucid/latest", "/api/v1/channels/latest/:ucid"].each do |route| ["/api/v1/channels/:ucid/latest", "/api/v1/channels/latest/:ucid"].each do |route|
get route do |env| get route do |env|
locale = LOCALES[env.get("locale").as(String)]?
env.response.content_type = "application/json" env.response.content_type = "application/json"
ucid = env.params.url["ucid"] ucid = env.params.url["ucid"]
@ -3163,7 +3165,7 @@ end
json.field "viewCount", video.views json.field "viewCount", video.views
json.field "published", video.published.to_unix json.field "published", video.published.to_unix
json.field "publishedText", "#{recode_date(video.published)} ago" json.field "publishedText", translate(locale, "`x` ago", recode_date(video.published, locale))
json.field "lengthSeconds", video.length_seconds json.field "lengthSeconds", video.length_seconds
json.field "liveNow", video.live_now json.field "liveNow", video.live_now
json.field "paid", video.paid json.field "paid", video.paid
@ -3351,7 +3353,7 @@ get "/api/v1/channels/search/:ucid" do |env|
json.field "viewCount", item.views json.field "viewCount", item.views
json.field "published", item.published.to_unix json.field "published", item.published.to_unix
json.field "publishedText", translate(locale, "`x` ago", recode_date(item.published)) json.field "publishedText", translate(locale, "`x` ago", recode_date(item.published, locale))
json.field "lengthSeconds", item.length_seconds json.field "lengthSeconds", item.length_seconds
json.field "liveNow", item.live_now json.field "liveNow", item.live_now
json.field "paid", item.paid json.field "paid", item.paid
@ -3480,7 +3482,7 @@ get "/api/v1/search" do |env|
json.field "viewCount", item.views json.field "viewCount", item.views
json.field "published", item.published.to_unix json.field "published", item.published.to_unix
json.field "publishedText", translate(locale, "`x` ago", recode_date(item.published)) json.field "publishedText", translate(locale, "`x` ago", recode_date(item.published, locale))
json.field "lengthSeconds", item.length_seconds json.field "lengthSeconds", item.length_seconds
json.field "liveNow", item.live_now json.field "liveNow", item.live_now
json.field "paid", item.paid json.field "paid", item.paid

View File

@ -184,7 +184,7 @@ def fetch_youtube_comments(id, db, continuation, proxies, format, locale, region
json.field "content", content json.field "content", content
json.field "contentHtml", content_html json.field "contentHtml", content_html
json.field "published", published.to_unix json.field "published", published.to_unix
json.field "publishedText", translate(locale, "`x` ago", recode_date(published)) json.field "publishedText", translate(locale, "`x` ago", recode_date(published, locale))
json.field "likeCount", node_comment["likeCount"] json.field "likeCount", node_comment["likeCount"]
json.field "commentId", node_comment["commentId"] json.field "commentId", node_comment["commentId"]
json.field "authorIsChannelOwner", node_comment["authorIsChannelOwner"] json.field "authorIsChannelOwner", node_comment["authorIsChannelOwner"]
@ -310,7 +310,7 @@ def template_youtube_comments(comments, locale)
<a class="#{child["authorIsChannelOwner"] == true ? "channel-owner" : ""}" href="#{child["authorUrl"]}">#{child["author"]}</a> <a class="#{child["authorIsChannelOwner"] == true ? "channel-owner" : ""}" href="#{child["authorUrl"]}">#{child["author"]}</a>
</b> </b>
<p style="white-space:pre-wrap">#{child["contentHtml"]}</p> <p style="white-space:pre-wrap">#{child["contentHtml"]}</p>
<span title="#{Time.unix(child["published"].as_i64).to_s(translate(locale, "%A %B %-d, %Y"))}">#{translate(locale, "`x` ago", recode_date(Time.unix(child["published"].as_i64)))} #{child["isEdited"] == true ? translate(locale, "(edited)") : ""}</span> <span title="#{Time.unix(child["published"].as_i64).to_s(translate(locale, "%A %B %-d, %Y"))}">#{translate(locale, "`x` ago", recode_date(Time.unix(child["published"].as_i64), locale))} #{child["isEdited"] == true ? translate(locale, "(edited)") : ""}</span>
| |
<a href="https://www.youtube.com/watch?v=#{comments["videoId"]}&lc=#{child["commentId"]}" title="#{translate(locale, "Youtube permalink of the comment")}">[YT]</a> <a href="https://www.youtube.com/watch?v=#{comments["videoId"]}&lc=#{child["commentId"]}" title="#{translate(locale, "Youtube permalink of the comment")}">[YT]</a>
| |
@ -375,7 +375,7 @@ def template_reddit_comments(root, locale)
<a href="javascript:void(0)" onclick="toggle_parent(this)">[ - ]</a> <a href="javascript:void(0)" onclick="toggle_parent(this)">[ - ]</a>
<b><a href="https://www.reddit.com/user/#{author}">#{author}</a></b> <b><a href="https://www.reddit.com/user/#{author}">#{author}</a></b>
#{translate(locale, "`x` points", number_with_separator(score))} #{translate(locale, "`x` points", number_with_separator(score))}
#{translate(locale, "`x` ago", recode_date(child.created_utc))} #{translate(locale, "`x` ago", recode_date(child.created_utc, locale))}
</p> </p>
<div> <div>
#{body_html} #{body_html}

View File

@ -136,31 +136,26 @@ def decode_date(string : String)
return Time.now - delta return Time.now - delta
end end
def recode_date(time : Time) def recode_date(time : Time, locale)
span = Time.now - time span = Time.now - time
if span.total_days > 365.0 if span.total_days > 365.0
span = {span.total_days / 365, "year"} span = translate(locale, "`x` years", (span.total_days.to_i / 365).to_s)
elsif span.total_days > 30.0 elsif span.total_days > 30.0
span = {span.total_days / 30, "month"} span = translate(locale, "`x` months", (span.total_days.to_i / 30).to_s)
elsif span.total_days > 7.0 elsif span.total_days > 7.0
span = {span.total_days / 7, "week"} span = translate(locale, "`x` weeks", (span.total_days.to_i / 7).to_s)
elsif span.total_hours > 24.0 elsif span.total_hours > 24.0
span = {span.total_days, "day"} span = translate(locale, "`x` days", (span.total_days).to_s)
elsif span.total_minutes > 60.0 elsif span.total_minutes > 60.0
span = {span.total_hours, "hour"} span = translate(locale, "`x` hours", (span.total_hours).to_s)
elsif span.total_seconds > 60.0 elsif span.total_seconds > 60.0
span = {span.total_minutes, "minute"} span = translate(locale, "`x` minutes", (span.total_minutes).to_s)
else else
span = {span.total_seconds, "second"} span = translate(locale, "`x` seconds", (span.total_seconds).to_s)
end end
span = {span[0].to_i, span[1]} return span
if span[0] > 1
span = {span[0], span[1] + "s"}
end
return span.join(" ")
end end
def number_with_separator(number) def number_with_separator(number)

View File

@ -66,7 +66,7 @@
</p> </p>
<% if Time.now - item.published > 1.minute %> <% if Time.now - item.published > 1.minute %>
<h5><%= translate(locale, "Shared `x` ago", recode_date(item.published)) %></h5> <h5><%= translate(locale, "Shared `x` ago", recode_date(item.published, locale)) %></h5>
<% end %> <% end %>
<% else %> <% else %>
<% if env.get?("user") && env.get("user").as(User).preferences.thin_mode %> <% if env.get?("user") && env.get("user").as(User).preferences.thin_mode %>
@ -100,7 +100,7 @@
</p> </p>
<% if Time.now - item.published > 1.minute %> <% if Time.now - item.published > 1.minute %>
<h5><%= translate(locale, "Shared `x` ago", recode_date(item.published)) %></h5> <h5><%= translate(locale, "Shared `x` ago", recode_date(item.published, locale)) %></h5>
<% end %> <% end %>
<% end %> <% end %>
</div> </div>