From cf93acb8ad44ba9cd486e8f6457d9fd9fbc041cc Mon Sep 17 00:00:00 2001 From: erdgeist Date: Sun, 12 Jul 2026 23:42:23 +0200 Subject: 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. --- app/helpers/datetime_helper.rb | 53 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 app/helpers/datetime_helper.rb (limited to 'app/helpers') 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 @@ +module DatetimeHelper + + def relative_distance(time, now = Time.current) + seconds = (now - time).abs + + case seconds + when 0...90 + [1, :minute] + when 90...45.minutes + [(seconds / 60).round, :minute] + when 45.minutes...24.hours + [(seconds / 1.hour).round, :hour] + when 24.hours...30.days + [(seconds / 1.day).round, :day] + when 30.days...365.days + [(seconds / 30.days).round, :month] + else + [(seconds / 365.days).round, :year] + end + end + + RELATIVE_TIME_UNIT_FORMS = { + en: { + minute: { one: "minute", other: "minutes" }, + hour: { one: "hour", other: "hours" }, + day: { one: "day", other: "days" }, + month: { one: "month", other: "months" }, + year: { one: "year", other: "years" }, + }, + de: { + # "other" here is the DATIVE plural needed after "vor" -- not the + # nominative plural you'd use standing alone ("3 Tage" vs "vor 3 + # Tagen"). All five happen to be the nominative plural + "n", since + # none of them already end in -n or -s -- a property of these five + # specific words, not a rule to extend to new units without checking. + minute: { one: "Minute", other: "Minuten" }, + hour: { one: "Stunde", other: "Stunden" }, + day: { one: "Tag", other: "Tagen" }, + month: { one: "Monat", other: "Monaten" }, + year: { one: "Jahr", other: "Jahren" }, + }, + }.freeze + + def relative_time_phrase(time, now = Time.current) + count, unit = relative_distance(time, now) + locale = I18n.locale.to_sym + forms = RELATIVE_TIME_UNIT_FORMS.fetch(locale, RELATIVE_TIME_UNIT_FORMS[:en]) + word = forms.fetch(unit).fetch(count == 1 ? :one : :other) + + locale == :de ? "vor #{count} #{word}" : "#{count} #{word} ago" + end + +end -- cgit v1.3