Skip to content

GH-18344 add Locale::addLikelySubtags/Locale::minimizeSubtags support. #18487

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

Merged
merged 1 commit into from
May 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ PHP NEWS
(David Carlier)
. Added null bytes presence in locale inputs for Locale class. (David Carlier)
. Added grapheme_levenshtein() function. (Yuya Hamada)
. Added Locale::addLikelySubtags/Locale::minimizeSubtags to handle
adding/removing likely subtags to a locale. (David Carlier)

- MySQLi:
. Fixed bugs GH-17900 and GH-8084 (calling mysqli::__construct twice).
Expand Down
2 changes: 2 additions & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ PHP 8.5 UPGRADE NOTES
NumberFormatter::CURRENCY_PLURAL, NumberFormatter::CASH_CURRENCY,
and NumberFormatter::CURRENCY_STANDARD for various currency-related
number formats.
. Added Locale::addLikelySubtags and Locale::minimizeSubtags to
handle likely tags on a given locale.

- XSL:
. The $namespace argument of XSLTProcessor::getParameter(),
Expand Down
10 changes: 10 additions & 0 deletions ext/intl/locale/locale.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,14 @@ public static function acceptFromHttp(string $header): string|false {}
* @alias locale_is_right_to_left
*/
public static function isRightToLeft(string $locale): bool {}

/**
* @alias locale_add_likely_subtags
*/
public static function addLikelySubtags(string $locale): string|false {}

/**
* @alias locale_minimize_subtags
*/
public static function minimizeSubtags(string $locale): string|false {}
}
12 changes: 11 additions & 1 deletion ext/intl/locale/locale_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions ext/intl/locale/locale_methods.c
Original file line number Diff line number Diff line change
Expand Up @@ -1639,3 +1639,49 @@ PHP_FUNCTION(locale_is_right_to_left)

RETURN_BOOL(uloc_isRightToLeft(locale));
}

PHP_FUNCTION(locale_add_likely_subtags)
{
char *locale, maximized_locale[ULOC_FULLNAME_CAPACITY];
UErrorCode status = 0;
size_t locale_len;

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_PATH(locale, locale_len)
ZEND_PARSE_PARAMETERS_END();

if (!locale_len) {
locale = (char *)intl_locale_get_default();
}

int32_t maximized_locale_len = uloc_addLikelySubtags(locale, maximized_locale, sizeof(maximized_locale), &status);
INTL_CHECK_STATUS(status, "locale_add_likely_subtags: invalid locale");
if (maximized_locale_len < 0) {
RETURN_FALSE;
}

RETURN_STRINGL(maximized_locale, maximized_locale_len);
}

PHP_FUNCTION(locale_minimize_subtags)
{
char *locale, minimized_locale[ULOC_FULLNAME_CAPACITY];
UErrorCode status = 0;
size_t locale_len;

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_PATH(locale, locale_len)
ZEND_PARSE_PARAMETERS_END();

if (!locale_len) {
locale = (char *)intl_locale_get_default();
}

int32_t minimized_locale_len = uloc_minimizeSubtags(locale, minimized_locale, sizeof(minimized_locale), &status);
INTL_CHECK_STATUS(status, "locale_minimize_subtags: invalid locale");
if (minimized_locale_len < 0) {
RETURN_FALSE;
}

RETURN_STRINGL(minimized_locale, minimized_locale_len);
}
4 changes: 4 additions & 0 deletions ext/intl/php_intl.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,10 @@ function locale_accept_from_http(string $header): string|false {}

function locale_is_right_to_left(string $locale): bool {}

function locale_add_likely_subtags(string $locale): string|false {}

function locale_minimize_subtags(string $locale): string|false {}

/* msgformat */

function msgfmt_create(string $locale, string $pattern): ?MessageFormatter {}
Expand Down
12 changes: 11 additions & 1 deletion ext/intl/php_intl_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions ext/intl/tests/locale_subtags.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
--TEST--
Locale::addLikelySubtags/Locale::minimizeSubtags usage
--EXTENSIONS--
intl
--FILE--
<?php
$locale = "en";
$max = Locale::addLikelySubtags($locale);
$min = Locale::minimizeSubtags($max);
var_dump($min === $locale);
var_dump($max !== $locale && strlen($max) > strlen($locale));
var_dump(Locale::addLikelySubtags($max) === $max);
var_dump(Locale::minimizeSubtags($locale) === $locale);
var_dump(Locale::addLikelySubtags("%%%invalid%%%locale%%%"));
var_dump(intl_get_error_message());
var_dump(Locale::minimizeSubtags("%%%Invalid%%%maximized%%%locale%%%"));
var_dump(intl_get_error_message());
var_dump(Locale::addLikelySubTags(str_repeat($locale, 1024)));
var_dump(intl_get_error_message());
var_dump(Locale::minimizeSubTags(str_repeat($max, 1024)));
var_dump(intl_get_error_message());
?>
--EXPECTF--
bool(true)
bool(true)
bool(true)
bool(true)
bool(false)
string(67) "locale_add_likely_subtags: invalid locale: U_ILLEGAL_ARGUMENT_ERROR"
bool(false)
string(65) "locale_minimize_subtags: invalid locale: U_ILLEGAL_ARGUMENT_ERROR"
bool(false)
string(%d) "locale_add_likely_subtags: invalid locale: %s"
bool(false)
string(%d) "locale_minimize_subtags: invalid locale: %s"