summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorerdgeist <erdgeist@erdgeist.org>2026-07-12 23:42:23 +0200
committererdgeist <erdgeist@erdgeist.org>2026-07-12 23:42:23 +0200
commitcf93acb8ad44ba9cd486e8f6457d9fd9fbc041cc (patch)
tree71ebf9abed3fe2b96262443e33da767315a5f7d2 /app
parentd320f8509751a886adb3ca723ebfafccaa8c191b (diff)
Add a locale-aware relative-time helper; drop stale locale overrides
Node.recently_changed and the new dashboard need "X ago" rendered correctly in both locales. Rails' own distance_of_time_in_words/ time_ago_in_words can't do this on their own -- the scope option changes which translation key is looked up, not the underlying grammar, and German's "vor" requires dative case, which is a different word form than the nominative plural Rails computes by default. relative_time_phrase/relative_distance is a small, from- scratch bucketing helper instead, with an explicit, hand-checked translation table per unit per locale. Also removes de.yml's datetime.distance_in_words and activerecord. errors blocks. Both used the old {{count}}/{{model}} interpolation syntax the current i18n gem no longer recognizes -- it silently leaves the literal placeholder text in the rendered output rather than erroring, which is why this went unnoticed until now. Both were shadowing rails-i18n's own current, correct translations for exactly these keys; deleting the local override lets the gem's version take over instead of maintaining a second, broken copy.
Diffstat (limited to 'app')
-rw-r--r--app/helpers/datetime_helper.rb53
1 files changed, 53 insertions, 0 deletions
diff --git a/app/helpers/datetime_helper.rb b/app/helpers/datetime_helper.rb
new file mode 100644
index 0000000..8497b1c
--- /dev/null
+++ b/app/helpers/datetime_helper.rb
@@ -0,0 +1,53 @@
1module DatetimeHelper
2
3 def relative_distance(time, now = Time.current)
4 seconds = (now - time).abs
5
6 case seconds
7 when 0...90
8 [1, :minute]
9 when 90...45.minutes
10 [(seconds / 60).round, :minute]
11 when 45.minutes...24.hours
12 [(seconds / 1.hour).round, :hour]
13 when 24.hours...30.days
14 [(seconds / 1.day).round, :day]
15 when 30.days...365.days
16 [(seconds / 30.days).round, :month]
17 else
18 [(seconds / 365.days).round, :year]
19 end
20 end
21
22 RELATIVE_TIME_UNIT_FORMS = {
23 en: {
24 minute: { one: "minute", other: "minutes" },
25 hour: { one: "hour", other: "hours" },
26 day: { one: "day", other: "days" },
27 month: { one: "month", other: "months" },
28 year: { one: "year", other: "years" },
29 },
30 de: {
31 # "other" here is the DATIVE plural needed after "vor" -- not the
32 # nominative plural you'd use standing alone ("3 Tage" vs "vor 3
33 # Tagen"). All five happen to be the nominative plural + "n", since
34 # none of them already end in -n or -s -- a property of these five
35 # specific words, not a rule to extend to new units without checking.
36 minute: { one: "Minute", other: "Minuten" },
37 hour: { one: "Stunde", other: "Stunden" },
38 day: { one: "Tag", other: "Tagen" },
39 month: { one: "Monat", other: "Monaten" },
40 year: { one: "Jahr", other: "Jahren" },
41 },
42 }.freeze
43
44 def relative_time_phrase(time, now = Time.current)
45 count, unit = relative_distance(time, now)
46 locale = I18n.locale.to_sym
47 forms = RELATIVE_TIME_UNIT_FORMS.fetch(locale, RELATIVE_TIME_UNIT_FORMS[:en])
48 word = forms.fetch(unit).fetch(count == 1 ? :one : :other)
49
50 locale == :de ? "vor #{count} #{word}" : "#{count} #{word} ago"
51 end
52
53end