MDL-84983 core: account for invalid locales in intl extension.
Recent PHP versions are now stricter on the `$locale` value passed to `IntlDateFormatter` class and will throw exceptions for invalid values (some of which may be present in our language packs). See: https://github.com/php/php-src/issues/12912
This commit is contained in:
committed by
Huong Nguyen
parent
89285b7efc
commit
2e90ab1ee8
+11
-1
@@ -819,7 +819,17 @@ class core_date {
|
||||
$calendar = IntlGregorianCalendar::createInstance($intltz);
|
||||
$calendar->setGregorianChange(PHP_INT_MIN);
|
||||
|
||||
return (new IntlDateFormatter($locale, $date_type, $time_type, $intltz, $calendar, $pattern))->format($timestamp);
|
||||
// We need to account for invalid locales here in recent PHP versions, and ensure we catch errors and switch
|
||||
// back to a default value where an invalid locale is provided.
|
||||
try {
|
||||
$formatter = new IntlDateFormatter($locale, $date_type, $time_type, $intltz, $calendar, $pattern);
|
||||
$result = $formatter->format($timestamp);
|
||||
} catch (Error $e) {
|
||||
$formatter = new IntlDateFormatter(null, $date_type, $time_type, $intltz, $calendar, $pattern);
|
||||
$result = $formatter->format($timestamp);
|
||||
}
|
||||
|
||||
return $result;
|
||||
};
|
||||
|
||||
// Same order as https://www.php.net/manual/en/function.strftime.php
|
||||
|
||||
@@ -787,6 +787,12 @@ final class date_test extends advanced_testcase {
|
||||
"%e %b %Y",
|
||||
" 9 окт. 2024",
|
||||
],
|
||||
'Invalid locale' => [
|
||||
1728487000,
|
||||
'xx',
|
||||
'%B %Y',
|
||||
'October 2024',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user