Skip to content

Conversation

Ayesh
Copy link
Contributor

@Ayesh Ayesh commented Jun 28, 2025

Adds a new polyfill to symfony/polyfill-intl-icu that provides the functionality of the new IntlListFormatter to PHP 7.2 and later.

Closes GH-530.

@Ayesh Ayesh force-pushed the feat/intllistformatter branch 2 times, most recently from fa352fc to 3b446ce Compare June 28, 2025 19:17
@Ayesh
Copy link
Contributor Author

Ayesh commented Jun 28, 2025

// cc @BogdanUngureanu, I could use your inputs/review here :)

]);
}

protected function getListPattern(): array {

Choose a reason for hiding this comment

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

Why not private?

private $width;

protected static $listPatterns = [
'en' => [

Choose a reason for hiding this comment

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

Maybe make it clear that this is en_US since it's using the Oxford comma?

) {
$exceptionClass = PHP_VERSION_ID >= 80000 ? \ValueError::class : \InvalidArgumentException::class;
if ($locale !== 'en' && strpos($locale, 'en') !== 0) {
throw new $exceptionClass('Invalid locale, only "en" and "en-*" locales are supported');

Choose a reason for hiding this comment

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

I have some mixed feelings about this. Since it's using the en-US set, for en-gb it would return a different result than the real class.

@stof
Copy link
Member

stof commented Jun 30, 2025

This should be added in symfony/polyfill-intl-icu (with the other formatters) instead of being a new package IMO

It only supports en locale. However, PHP applications that require this package can extend the namespaced polyfill
(\Symfony\Polyfill\Intl\ListFormatter\IntlListFormatter) and add their own list patterns by extending the static variable (\Symfony\Polyfill\Intl\ListFormatter\IntlListFormatter::$listPatterns) that holds these patterns.

Extending the internal class is not covered by BC and is not a supported use case.
Projects wanting the full support for ICU formatting should install ext-intl.

@stof
Copy link
Member

stof commented Sep 19, 2025

@Ayesh do you plan to finish this PR (by taking review comments into account) ?

@Ayesh
Copy link
Contributor Author

Ayesh commented Sep 19, 2025

Thank you for your review Christophe. I will try and submit fixed addressing the reviewed points by this weekend.

@Ayesh Ayesh force-pushed the feat/intllistformatter branch from 3b446ce to ad6b26c Compare September 22, 2025 08:47
@Ayesh Ayesh changed the title [Intl] Add PHP 8.5 IntlListFormatter as installable polyfill [Intl] Add PHP 8.5 IntlListFormatter to ICU polyfill Sep 22, 2025
@Ayesh Ayesh force-pushed the feat/intllistformatter branch 2 times, most recently from 0518c84 to c888ccc Compare September 22, 2025 08:54
@Ayesh
Copy link
Contributor Author

Ayesh commented Sep 22, 2025

Thank you for your reviews.

I changed the PR, so that this now proposes to add IntlListFormatter to the ICU package. I also agree that it makes more sense, as opposed to having a separate installable package.

I also made it so that the class is self-contained, and not extendable anymore (making protected elements private, formatting pattern changed to a const, etc).

@Ayesh Ayesh requested a review from stof September 22, 2025 09:01
return strtr($pattern[2], ['{0}' => (string) $strings[0], '{1}' => (string) $strings[1]]);
}

if ($itemCount === 3) {
Copy link
Member

Choose a reason for hiding this comment

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

You could remove this case. It can be covered by the same code that for 4 items or more (the for loop will simply do no iterations, which is not an error)

*/
class IntlListFormatterTest extends TestCase
{
public function testUnsupportedLocales()
Copy link
Member

Choose a reason for hiding this comment

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

Testing support locales should be done in a separate test than testUnsupportedLocales to make the intent of the tests clear.

Adds a new polyfill to `symfony/polyfill-intl-icu` that provides the
functionality of the new `IntlListFormatter` to PHP 7.2 and later.

 - [ICU listPatterns](https://github.com/unicode-org/cldr-json/blob/main/cldr-json/cldr-misc-full/main/en/listPatterns.json)
 - [php-src commit](php/php-src@3f7545245)
 - [PHP.Watch: IntlListFormatter](https://php.watch/versions/8.5/IntlListFormatter)

Closes symfonyGH-530.
@Ayesh Ayesh force-pushed the feat/intllistformatter branch from c888ccc to 0156b53 Compare September 22, 2025 17:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add IntlListFormatter
3 participants