Repository navigation
Add relative_time_in_words helper to ActionView - #55405
Conversation
9814f6c to
9410f4b
Compare
| 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]) |
There was a problem hiding this comment.
| 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.
There was a problem hiding this comment.
Ah, I see you essentially followed the same pattern.
There was a problem hiding this comment.
I simplified the implementation a little bit anyway.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Yes, that's what I figured, so I left the .except.
There was a problem hiding this comment.
I think it's really niche, so probably not worth it.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
@A1bi if you're facing this problem, I encourage you to open a PR with my suggestion.
There was a problem hiding this comment.
@A1bi if you're facing this problem, I encourage you to open a PR with my suggestion.
There was a problem hiding this comment.
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?
9410f4b to
3920483
Compare
3920483 to
46c5bc7
Compare
this adds translations used by the `relative_time_in_words` helper added in rails/rails#55405.
|
Love it! Thanks! |
this adds translations used by the `relative_time_in_words` helper added in rails/rails#55405.
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_wordsand also because returning"in <future time>"from a helper calledtime_agodoesn't make sense.Detail
This Pull Request adds a new helper
relative_time_in_wordsto ActionView.Checklist
Before submitting the PR make sure the following are checked:
[Fix #issue-number]