Skip to content

ext/intl: Add IntlNumberFormatter skeleton based formatter. - #24266

Open
devnexen wants to merge 5 commits into
php:masterfrom
devnexen:gh24165
Open

devnexen wants to merge 5 commits into
php:masterfrom
devnexen:gh24165

Conversation

@devnexen

Copy link
Copy Markdown
Member

Add IntlNumberFormatter::createFromSkeleton() so format() can render units such as "5 km/h", which NumberFormatter cannot do. Needs ICU 63+.

Close GH-24165

Add IntlNumberFormatter::createFromSkeleton() so format() can render
units such as "5 km/h", which NumberFormatter cannot do. Needs ICU 63+.

Close phpGH-24165
@devnexen
devnexen marked this pull request as ready for review October 11, 2026 20:30
@LamentXU123 LamentXU123 self-assigned this Oct 12, 2026

@LamentXU123 LamentXU123 left a comment

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.

By the way, I think you can add yourself to the CREDITS file of this extension.

INTL_CHECK_STATUS_OR_NULL(status, "Number formatter creation failed");

object_init_ex(return_value, IntlNumberFormatter_ce_ptr);
NUMBERFORMATTER_OBJECT(Z_INTL_INTLNUMBERFORMATTER_P(return_value)) = new LocalizedNumberFormatter(nf.locale(icu::Locale(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.

I think you might do some checks here

--TEST--
IntlNumberFormatter preserves the region of a locale with a POSIX modifier
--EXTENSIONS--
intl
--SKIPIF--
<?php
if (!class_exists('IntlNumberFormatter')) {
    die('skip IntlNumberFormatter not available');
}
?>
--FILE--
<?php
$skeleton = 'measure-unit/length-kilometer unit-width-full-name';

$plain = IntlNumberFormatter::createFromSkeleton($skeleton, 'pt_PT');
$modified = IntlNumberFormatter::createFromSkeleton($skeleton, 'pt_PT@euro');

var_dump($plain->format(12345.5) === $modified->format(12345.5));
?>
--EXPECT--
bool(true)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

yes it is a good point.

U_CFUNC PHP_METHOD(IntlNumberFormatter, format)
{
zval *value;
NUMBERFORMATTER_METHOD_INIT_VARS;

@LamentXU123 LamentXU123 Oct 13, 2026 •

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 you should move this one after ZEND_PARSE_PARAMETERS_END considering about global error state ?

@LamentXU123 LamentXU123 left a comment

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.

Thanks. This is indeed useful.

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.

[ext/intl] Expose unit formatting in NumberFormatter

2 participants