-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Intl] Add DateIntervalFormatter
#39912
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
0169445
to
5189d2d
Compare
src/Symfony/Component/Translation/Formatter/DateIntervalFormatter.php
Outdated
Show resolved
Hide resolved
there's an interesting ICU concept: listPattern relative/duration time formats ideally we'd implement this at the ICU message format level 😁 but that's far fetched. overall, having a generic localized interval formatter in the Intl component seems more useful. |
I agree and we could use it in the Validator component too for #33401 |
2888345
to
7f6cbba
Compare
b43f917
to
0ed15bd
Compare
src/Symfony/Component/Intl/Tests/DateIntervalFormatter/DateIntervalFormatterTest.php
Show resolved
Hide resolved
1c09134
to
894cc36
Compare
Ready for review |
894cc36
to
86a078a
Compare
use Twig\Extension\AbstractExtension; | ||
use Twig\TwigFilter; | ||
|
||
class IntlExtension extends AbstractExtension |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about doing this in https://github.com/twigphp/intl-extra/blob/2.x/IntlExtension.php
instead?
Currently it may conflict with https://github.com/twigphp/twig-extra-bundle/blob/2.x/Resources/config/intl.php.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think @HeahDude's idea is a good one. Can you please remove that code from this PR and move it to twig instead?
FYI https://carbon.nesbot.com/docs/#api-interval provides a fully localized version of DateInterval with |
DateIntervalFormatter
Open to finish this PR @YaFou ? |
For me, it is finished. I saw comments which suggest to add ICU but I don't think we have to add something to big to Symfony core. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm mixed here: I get the value, but what about i18n? Is the language designed to be an implementation detail or should the interface allow declaring it?
In the end, my question ends up as: shouldn't we delegate this to a 3rd party lib, eg Carbon?
use Twig\Extension\AbstractExtension; | ||
use Twig\TwigFilter; | ||
|
||
class IntlExtension extends AbstractExtension |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think @HeahDude's idea is a good one. Can you please remove that code from this PR and move it to twig instead?
@@ -158,5 +160,8 @@ | |||
->args([service(ContainerInterface::class)]) | |||
->tag('container.service_subscriber', ['id' => 'translator']) | |||
->tag('kernel.cache_warmer') | |||
|
|||
->set('translation.formatter.date_interval', DateIntervalFormatter::class) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't get the need for this service
return static function (ContainerConfigurator $container) { | ||
$container->services() | ||
->set('intl.date_interval_formatter', DateIntervalFormatter::class) | ||
->alias(DateIntervalFormatterInterface::class, 'intl.date_interval_formatter') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
alias should be unindented by one
@@ -574,6 +575,10 @@ public function load(array $configs, ContainerBuilder $container) | |||
'kernel.locale_aware', | |||
'kernel.reset', | |||
]); | |||
|
|||
if (class_exists(DateIntervalFormatterInterface::class)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
interface_exists, since it's not a class but an interface
5.4 | ||
--- | ||
|
||
* Add `DateIntervalFormatter` to humanize a `DateInterval` or a difference between two `DateTimeInterface` objects |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing space at start of line
/** | ||
* @param \DateInterval|string $interval | ||
*/ | ||
public function formatInterval($interval, int $precision = 0): string; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is $precision supposed to mean? reading the interface should provide a clue
Having something like this in core without i18n is a no-go for me. As soon as we will merge it, people will start asking for it and they will be right. |
You can set a precision. The precision cuts the formatted content to a given number of elements: