[php-src] PHP-8.5: ext/intl: Fix Locale::lookup() fallback for empty language tags (#25462)
| From: | Weilin Du | Date: | Tue, 17 Nov 2026 11:26:45 +0000 |
| Subject: | [php-src] PHP-8.5: ext/intl: Fix Locale::lookup() fallback for empty language tags (#25462) | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-143760@lists.php.net to get a copy of this message | ||
Author: Weilin Du (LamentXU123)
Date: 2026-11-17T19:16:37+08:00
Commit: https://github.com/php/php-src/commit/5645e5b0803e4e3bd5a5e98d199fe79acd45228f
Raw diff: https://github.com/php/php-src/commit/5645e5b0803e4e3bd5a5e98d199fe79acd45228f.diff
ext/intl: Fix Locale::lookup() fallback for empty language tags (#25462)
Return the supplied default locale when the language tag list is empty.
Locale::lookup([], 'fr_FR', false, 'en_US') now returns 'en_US'
instead
of an empty string.
Closes #25462
Changed paths:
A ext/intl/tests/locale_lookup_empty_language_tags.phpt
M NEWS
M ext/intl/locale/locale_methods.c
Diff:
diff --git a/NEWS b/NEWS
index 84e8fdd0473b..5fde6a46c6bb 100644
--- a/NEWS
+++ b/NEWS
@@ -174,6 +174,8 @@ PHP NEWS
- Intl:
. Fixed IntlDateFormatter parse functions truncating an $offset below
INT_MIN. (Marc Bennewitz)
+ . Fixed Locale::lookup() ignoring the default locale when the language tag
+ list is empty. (Weilin Du)
- JSON:
. Fixed json_decode() and json_validate() error messages. (lacatoire)
diff --git a/ext/intl/locale/locale_methods.c b/ext/intl/locale/locale_methods.c
index 2643f4920878..1b61794d67fb 100644
--- a/ext/intl/locale/locale_methods.c
+++ b/ext/intl/locale/locale_methods.c
@@ -1560,6 +1560,9 @@ PHP_FUNCTION(locale_lookup)
INTL_CHECK_LOCALE_LEN(loc_range_len);
if( !hash_arr || zend_hash_num_elements( hash_arr ) == 0 ) {
+ if (fallback_loc_str) {
+ RETURN_STR_COPY(fallback_loc_str);
+ }
RETURN_EMPTY_STRING();
}
diff --git a/ext/intl/tests/locale_lookup_empty_language_tags.phpt
b/ext/intl/tests/locale_lookup_empty_language_tags.phpt
new file mode 100644
index 000000000000..b0bb2d1e6993
--- /dev/null
+++ b/ext/intl/tests/locale_lookup_empty_language_tags.phpt
@@ -0,0 +1,42 @@
+--TEST--
+Locale::lookup() honors the default locale for an empty language tag list
+--EXTENSIONS--
+intl
+--FILE--
+<?php
+foreach (['locale_lookup', [Locale::class, 'lookup']] as $lookup) {
+ foreach ([false, true] as $canonicalize) {
+ var_dump($lookup([], 'fr_FR', $canonicalize, 'en-US'));
+ var_dump($lookup([], '', $canonicalize, 'en-US'));
+ var_dump($lookup([], 'fr_FR', $canonicalize, ''));
+ var_dump($lookup([], 'fr_FR', $canonicalize, null));
+ var_dump($lookup([], 'fr_FR', $canonicalize));
+ var_dump(intl_get_error_code());
+ }
+}
+?>
+--EXPECT--
+string(5) "en-US"
+string(5) "en-US"
+string(0) ""
+string(0) ""
+string(0) ""
+int(0)
+string(5) "en-US"
+string(5) "en-US"
+string(0) ""
+string(0) ""
+string(0) ""
+int(0)
+string(5) "en-US"
+string(5) "en-US"
+string(0) ""
+string(0) ""
+string(0) ""
+int(0)
+string(5) "en-US"
+string(5) "en-US"
+string(0) ""
+string(0) ""
+string(0) ""
+int(0)