Skip to content

Commit bbac6f5

Browse files
authored
GH-18344 add Locale::addLikelySubtags/Locale::minimizeSubtags support. (#18487)
from a minimized locale, addLikelySubtags augments it with likely subtags so no changes is the locale is already maximized e.g. `en_Latn_US`, minimizeSubtags on the other hand does the opposite operation.
1 parent 07959fc commit bbac6f5

File tree

8 files changed

+121
-2
lines changed

8 files changed

+121
-2
lines changed

NEWS

+2
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ PHP NEWS
9494
(David Carlier)
9595
. Added null bytes presence in locale inputs for Locale class. (David Carlier)
9696
. Added grapheme_levenshtein() function. (Yuya Hamada)
97+
. Added Locale::addLikelySubtags/Locale::minimizeSubtags to handle
98+
adding/removing likely subtags to a locale. (David Carlier)
9799

98100
- MySQLi:
99101
. Fixed bugs GH-17900 and GH-8084 (calling mysqli::__construct twice).

UPGRADING

+2
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ PHP 8.5 UPGRADE NOTES
178178
NumberFormatter::CURRENCY_PLURAL, NumberFormatter::CASH_CURRENCY,
179179
and NumberFormatter::CURRENCY_STANDARD for various currency-related
180180
number formats.
181+
. Added Locale::addLikelySubtags and Locale::minimizeSubtags to
182+
handle likely tags on a given locale.
181183

182184
- XSL:
183185
. The $namespace argument of XSLTProcessor::getParameter(),

ext/intl/locale/locale.stub.php

+10
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,14 @@ public static function acceptFromHttp(string $header): string|false {}
138138
* @alias locale_is_right_to_left
139139
*/
140140
public static function isRightToLeft(string $locale): bool {}
141+
142+
/**
143+
* @alias locale_add_likely_subtags
144+
*/
145+
public static function addLikelySubtags(string $locale): string|false {}
146+
147+
/**
148+
* @alias locale_minimize_subtags
149+
*/
150+
public static function minimizeSubtags(string $locale): string|false {}
141151
}

ext/intl/locale/locale_arginfo.h

+11-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/intl/locale/locale_methods.c

+46
Original file line numberDiff line numberDiff line change
@@ -1639,3 +1639,49 @@ PHP_FUNCTION(locale_is_right_to_left)
16391639

16401640
RETURN_BOOL(uloc_isRightToLeft(locale));
16411641
}
1642+
1643+
PHP_FUNCTION(locale_add_likely_subtags)
1644+
{
1645+
char *locale, maximized_locale[ULOC_FULLNAME_CAPACITY];
1646+
UErrorCode status = 0;
1647+
size_t locale_len;
1648+
1649+
ZEND_PARSE_PARAMETERS_START(1, 1)
1650+
Z_PARAM_PATH(locale, locale_len)
1651+
ZEND_PARSE_PARAMETERS_END();
1652+
1653+
if (!locale_len) {
1654+
locale = (char *)intl_locale_get_default();
1655+
}
1656+
1657+
int32_t maximized_locale_len = uloc_addLikelySubtags(locale, maximized_locale, sizeof(maximized_locale), &status);
1658+
INTL_CHECK_STATUS(status, "locale_add_likely_subtags: invalid locale");
1659+
if (maximized_locale_len < 0) {
1660+
RETURN_FALSE;
1661+
}
1662+
1663+
RETURN_STRINGL(maximized_locale, maximized_locale_len);
1664+
}
1665+
1666+
PHP_FUNCTION(locale_minimize_subtags)
1667+
{
1668+
char *locale, minimized_locale[ULOC_FULLNAME_CAPACITY];
1669+
UErrorCode status = 0;
1670+
size_t locale_len;
1671+
1672+
ZEND_PARSE_PARAMETERS_START(1, 1)
1673+
Z_PARAM_PATH(locale, locale_len)
1674+
ZEND_PARSE_PARAMETERS_END();
1675+
1676+
if (!locale_len) {
1677+
locale = (char *)intl_locale_get_default();
1678+
}
1679+
1680+
int32_t minimized_locale_len = uloc_minimizeSubtags(locale, minimized_locale, sizeof(minimized_locale), &status);
1681+
INTL_CHECK_STATUS(status, "locale_minimize_subtags: invalid locale");
1682+
if (minimized_locale_len < 0) {
1683+
RETURN_FALSE;
1684+
}
1685+
1686+
RETURN_STRINGL(minimized_locale, minimized_locale_len);
1687+
}

ext/intl/php_intl.stub.php

+4
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,10 @@ function locale_accept_from_http(string $header): string|false {}
505505

506506
function locale_is_right_to_left(string $locale): bool {}
507507

508+
function locale_add_likely_subtags(string $locale): string|false {}
509+
510+
function locale_minimize_subtags(string $locale): string|false {}
511+
508512
/* msgformat */
509513

510514
function msgfmt_create(string $locale, string $pattern): ?MessageFormatter {}

ext/intl/php_intl_arginfo.h

+11-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/intl/tests/locale_subtags.phpt

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
--TEST--
2+
Locale::addLikelySubtags/Locale::minimizeSubtags usage
3+
--EXTENSIONS--
4+
intl
5+
--FILE--
6+
<?php
7+
$locale = "en";
8+
$max = Locale::addLikelySubtags($locale);
9+
$min = Locale::minimizeSubtags($max);
10+
var_dump($min === $locale);
11+
var_dump($max !== $locale && strlen($max) > strlen($locale));
12+
var_dump(Locale::addLikelySubtags($max) === $max);
13+
var_dump(Locale::minimizeSubtags($locale) === $locale);
14+
var_dump(Locale::addLikelySubtags("%%%invalid%%%locale%%%"));
15+
var_dump(intl_get_error_message());
16+
var_dump(Locale::minimizeSubtags("%%%Invalid%%%maximized%%%locale%%%"));
17+
var_dump(intl_get_error_message());
18+
var_dump(Locale::addLikelySubTags(str_repeat($locale, 1024)));
19+
var_dump(intl_get_error_message());
20+
var_dump(Locale::minimizeSubTags(str_repeat($max, 1024)));
21+
var_dump(intl_get_error_message());
22+
?>
23+
--EXPECTF--
24+
bool(true)
25+
bool(true)
26+
bool(true)
27+
bool(true)
28+
bool(false)
29+
string(67) "locale_add_likely_subtags: invalid locale: U_ILLEGAL_ARGUMENT_ERROR"
30+
bool(false)
31+
string(65) "locale_minimize_subtags: invalid locale: U_ILLEGAL_ARGUMENT_ERROR"
32+
bool(false)
33+
string(%d) "locale_add_likely_subtags: invalid locale: %s"
34+
bool(false)
35+
string(%d) "locale_minimize_subtags: invalid locale: %s"

0 commit comments

Comments
 (0)