Skip to content

Add relative_time_in_words helper to ActionView - #55405

Merged
byroot merged 1 commit into
rails:mainfrom
MatheusRich:relative-time-in-words
Jul 28, 2025
Merged

byroot merged 1 commit into
rails:mainfrom
MatheusRich:relative-time-in-words

Conversation

@MatheusRich

Copy link
Copy Markdown
Contributor

Motivation / Background

A similar proposal was made in #52547. The need for the feature is here, but I proposed a new method not to complicate time_ago_in_words and also because returning "in <future time>" from a helper called time_ago doesn't make sense.

Detail

This Pull Request adds a new helper relative_time_in_words to ActionView.

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Unrelated changes should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

Comment on lines +199 to +203
options = { scope: :'datetime.relative' }.merge(options)
time = time_ago_in_words(from_time, options.except(:scope))
key = from_time > Time.now ? :future : :past

I18n.t(key, time: time, scope: options[:scope], locale: options[:locale])

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
options = { scope: :'datetime.relative' }.merge(options)
time = time_ago_in_words(from_time, options.except(:scope))
key = from_time > Time.now ? :future : :past
I18n.t(key, time: time, scope: options[:scope], locale: options[:locale])
time = time_ago_in_words(from_time, options)
key = from_time > Time.now ? :future : :past
I18n.t(
key,
time: time,
scope: options.fetch(:scope, :'datetime.relative'),
locale: options[:locale],
)

Is it really important not to forward the scope options? Because I think this could be streamlined.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see you essentially followed the same pattern.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I simplified the implementation a little bit anyway.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both this method and the time_ago one have scopes, so they'll use the same key if I forward. I'd love being able to pass a scope for each, but idk if there's a pattern for that already.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's what I figured, so I left the .except.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's really niche, so probably not worth it.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's actually not that niche. In the German language the output of relative_time_in_words is grammatically incorrect, because it uses the default translation for distance_of_time_in_words (e.g. "vor ein Jahr" and "vor drei Jahre" should be "vor einem Jahr" and "vor drei Jahren"). You need to have separate translations for distance_of_time_in_words if they are used in conjunction with a preposition from relative_time_in_words.

You could choose a different phrasing in relative_time_in_words's translation to make it grammatically correct, but nothing really works without sounding weird in German. I can imagine you have the same problem with other languages.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@A1bi if you're facing this problem, I encourage you to open a PR with my suggestion.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@A1bi if you're facing this problem, I encourage you to open a PR with my suggestion.

@A1bi A1bi Dec 29, 2025 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After thinking a little more about this, I came to the conclusion it's probably best if we had completely independent translations for relative_time_in_words, because it might not work in all languages to simply have a string interpolation containing the translation of distance_of_time_in_words.

I imagine something like this in en.yml:

datetime:
  relative_time_in_words:
    future:
      ...
      x_minutes:
        one:   "in 1 minute"
        other: "in %{count} minutes"
      ...
    past:
      ...
      x_minutes:
        one:   "1 minute ago"
        other: "%{count} minutes ago"
      ...

This would obviously introduce a breaking change, so I'm not sure how to go about that. What do you think?

@byroot
byroot force-pushed the relative-time-in-words branch from 9410f4b to 3920483 Compare July 28, 2025 13:27
@byroot
byroot force-pushed the relative-time-in-words branch from 3920483 to 46c5bc7 Compare July 28, 2025 13:27
@byroot
byroot enabled auto-merge July 28, 2025 13:28
@byroot
byroot merged commit 3e54482 into rails:main Jul 28, 2025
glaszig added a commit to glaszig/rails-i18n that referenced this pull request Aug 2, 2025
this adds translations used by the `relative_time_in_words` helper
added in rails/rails#55405.
@adrianthedev

Copy link
Copy Markdown
Contributor

Love it! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants