Skip to content

[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

Closed
wants to merge 1 commit into from

Conversation

YaFou
Copy link
Contributor

@YaFou YaFou commented Jan 20, 2021

Q A
Branch? 5.x
Bug fix? no
New feature? yes
Deprecations? no
Tickets Fix #29929
License MIT
Doc PR TODO
$formatter = new DateIntervalFormatter();
$formatter->formatInterval(new \DateInterval('P1DT5H2M')); // one day, 5 hours and 2 minutes

You can set a precision. The precision cuts the formatted content to a given number of elements:

$formatter = new DateIntervalFormatter();
$formatter->formatInterval(new \DateInterval('P1DT20H2M'), 2); // one day and 20 hours

@carsonbot carsonbot changed the title [Translation] [WIP] Add DateIntervalFormatter [Translator] [WIP] Add DateIntervalFormatter Jan 20, 2021
@YaFou YaFou force-pushed the date-interval-formatter branch 2 times, most recently from 0169445 to 5189d2d Compare January 20, 2021 20:54
@ro0NL
Copy link
Contributor

ro0NL commented Jan 21, 2021

there's an interesting ICU concept: listPattern
https://github.com/unicode-org/icu/blob/6092942e23785d85de5b40ea2f47768f7fa9345b/icu4c/source/data/locales/en.txt#L2007

relative/duration time formats
https://github.com/unicode-org/icu/blob/6092942e23785d85de5b40ea2f47768f7fa9345b/icu4c/source/data/locales/en.txt#L1441-L1456
https://github.com/unicode-org/icu/blob/6092942e23785d85de5b40ea2f47768f7fa9345b/icu4c/source/data/unit/en.txt#L286

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.

@fancyweb
Copy link
Contributor

fancyweb commented Jan 21, 2021

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

@YaFou YaFou force-pushed the date-interval-formatter branch from 2888345 to 7f6cbba Compare January 21, 2021 10:04
@YaFou YaFou requested a review from yceruto as a code owner January 21, 2021 10:04
@YaFou YaFou force-pushed the date-interval-formatter branch 2 times, most recently from b43f917 to 0ed15bd Compare January 21, 2021 10:08
@nicolas-grekas nicolas-grekas added this to the 5.x milestone Jan 21, 2021
@YaFou YaFou changed the title [Translator] [WIP] Add DateIntervalFormatter [Intl] [WIP] Add DateIntervalFormatter Jun 26, 2021
@YaFou YaFou force-pushed the date-interval-formatter branch 5 times, most recently from 1c09134 to 894cc36 Compare June 26, 2021 09:01
@YaFou
Copy link
Contributor Author

YaFou commented Jun 26, 2021

Ready for review

@YaFou YaFou changed the title [Intl] [WIP] Add DateIntervalFormatter [Intl] Add DateIntervalFormatter Jun 26, 2021
@YaFou YaFou requested a review from OskarStark June 26, 2021 09:03
@YaFou YaFou force-pushed the date-interval-formatter branch from 894cc36 to 86a078a Compare June 26, 2021 09:05
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;

class IntlExtension extends AbstractExtension
Copy link
Contributor

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.

Copy link
Member

@nicolas-grekas nicolas-grekas Oct 18, 2021

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?

@sstok
Copy link
Contributor

sstok commented Jul 4, 2021

FYI https://carbon.nesbot.com/docs/#api-interval provides a fully localized version of DateInterval with forHumans().

@OskarStark OskarStark changed the title [Intl] Add DateIntervalFormatter [Intl] Add DateIntervalFormatter Aug 4, 2021
@OskarStark
Copy link
Contributor

Open to finish this PR @YaFou ?

@YaFou
Copy link
Contributor Author

YaFou commented Aug 4, 2021

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.

Copy link
Member

@nicolas-grekas nicolas-grekas left a 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
Copy link
Member

@nicolas-grekas nicolas-grekas Oct 18, 2021

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)
Copy link
Member

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')
Copy link
Member

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)) {
Copy link
Member

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
Copy link
Member

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;
Copy link
Member

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

@fabpot
Copy link
Member

fabpot commented Oct 30, 2021

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.
Let's close for now.

@fabpot fabpot closed this Oct 30, 2021
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.

[Translation] Add human readable DateInterval
9 participants