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 ++++++++++++++++++++++ config/locales/de.yml | 70 ++--------------------------- config/locales/en.yml | 4 ++ test/models/helpers/datetime_helper_test.rb | 19 ++++++++ 4 files changed, 80 insertions(+), 66 deletions(-) create mode 100644 app/helpers/datetime_helper.rb create mode 100644 test/models/helpers/datetime_helper_test.rb 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 diff --git a/config/locales/de.yml b/config/locales/de.yml index 1f2222c..550ccb3 100644 --- a/config/locales/de.yml +++ b/config/locales/de.yml @@ -14,6 +14,10 @@ de: open_days_label: "Offene Tage" upcoming_events: "Weitere Termine" open_today: "Das Chaos, lokal und heute offen" + published: "veröffentlicht" + published_by: "veröffentlicht durch %{editor}" + editor_self: "du" + publisher_self: "dich" date: formats: default: "%d.%m.%Y" @@ -29,7 +33,6 @@ de: - :day - :month - :year - time: formats: default: "%A, %e. %B %Y, %H:%M Uhr" @@ -41,42 +44,6 @@ de: am: "vormittags" pm: "nachmittags" - datetime: - distance_in_words: - half_a_minute: 'eine halbe Minute' - less_than_x_seconds: - zero: 'weniger als 1 Sekunde' - one: 'weniger als 1 Sekunde' - other: 'weniger als {{count}} Sekunden' - x_seconds: - one: '1 Sekunde' - other: '{{count}} Sekunden' - less_than_x_minutes: - zero: 'weniger als 1 Minute' - one: 'weniger als eine Minute' - other: 'weniger als {{count}} Minuten' - x_minutes: - one: '1 Minute' - other: '{{count}} Minuten' - about_x_hours: - one: 'etwa 1 Stunde' - other: 'etwa {{count}} Stunden' - x_days: - one: '1 Tag' - other: '{{count}} Tage' - about_x_months: - one: 'etwa 1 Monat' - other: 'etwa {{count}} Monate' - x_months: - one: '1 Monat' - other: '{{count}} Monate' - about_x_years: - one: 'etwa 1 Jahr' - other: 'etwa {{count}} Jahre' - over_x_years: - one: 'mehr als 1 Jahr' - other: 'mehr als {{count}} Jahre' - number: format: precision: 2 @@ -105,35 +72,6 @@ de: sentence_connector: "und" skip_last_comma: true - activerecord: - errors: - template: - header: - one: 1 error prohibited this {{model}} from being saved - other: "{{count}} errors prohibited this {{model}} from being saved" - body: "There were problems with the following fields:" - - messages: - inclusion: "is not included in the list" - exclusion: "is reserved" - invalid: "is invalid" - confirmation: "doesn't match confirmation" - accepted: "must be accepted" - empty: "can't be empty" - blank: "can't be blank" - too_long: "is too long (maximum is {{count}} characters)" - too_short: "is too short (minimum is {{count}} characters)" - wrong_length: "is the wrong length (should be {{count}} characters)" - taken: "has already been taken" - not_a_number: "is not a number" - greater_than: "must be greater than {{count}}" - greater_than_or_equal_to: "must be greater than or equal to {{count}}" - equal_to: "must be equal to {{count}}" - less_than: "must be less than {{count}}" - less_than_or_equal_to: "must be less than or equal to {{count}}" - odd: "must be odd" - even: "must be even" - will_paginate: previous_label: "← Zurück" previous_aria_label: "Vorherige Seite" diff --git a/config/locales/en.yml b/config/locales/en.yml index 59c7304..73271ed 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -15,6 +15,10 @@ en: open_days_label: "Open days" upcoming_events: "Upcoming events" open_today: "Open today" + published: "published" + published_by: "published by %{editor}" + publisher_self: "you" + editor_self: "you" time: formats: diff --git a/test/models/helpers/datetime_helper_test.rb b/test/models/helpers/datetime_helper_test.rb new file mode 100644 index 0000000..a24e5d3 --- /dev/null +++ b/test/models/helpers/datetime_helper_test.rb @@ -0,0 +1,19 @@ +require 'test_helper' + +class DatetimeHelperTest < ActionView::TestCase + test "relative_time_phrase pluralizes correctly in English and German" do + travel_to Time.zone.parse("2026-07-12 12:00:00") do + I18n.with_locale(:en) do + assert_equal "1 day ago", relative_time_phrase(1.day.ago) + assert_equal "3 days ago", relative_time_phrase(3.days.ago) + end + I18n.with_locale(:de) do + assert_equal "vor 1 Tag", relative_time_phrase(1.day.ago) + assert_equal "vor 3 Tagen", relative_time_phrase(3.days.ago) + assert_equal "vor 1 Monat", relative_time_phrase(30.days.ago) + assert_equal "vor 2 Monaten", relative_time_phrase(45.days.ago) + assert_equal "vor 2 Jahren", relative_time_phrase(2.years.ago) + end + end + end +end -- cgit v1.3