Skip to content

Commit

Permalink
Set locale to default for number and date formatters.
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentmuller committed Jun 7, 2024
1 parent de4afec commit 0d7f01a
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
1 change: 1 addition & 0 deletions .github/workflows/php_unit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: '8.2'
ini-values: intl.default_locale=fr_CH
coverage: xdebug

- name: Install dependencies
Expand Down
7 changes: 6 additions & 1 deletion src/Utils/FormatUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,9 @@ public static function getDateFormatter(
?string $pattern = null,
\DateTimeZone|string|null $timezone = null
): \IntlDateFormatter {
if (self::DEFAULT_LOCALE !== \Locale::getDefault()) {
\Locale::setDefault(self::DEFAULT_LOCALE);
}
$locale = \Locale::getDefault();
$dateType ??= self::DATE_TYPE;
$timeType ??= self::TIME_TYPE;
Expand Down Expand Up @@ -268,7 +271,9 @@ public static function getNumberFormatter(
): \NumberFormatter {
$hash = self::hashCode($style, $digits, $roundingMode, $percentSymbol);
if (!isset(self::$numberFormatters[$hash])) {
\Locale::setDefault(self::DEFAULT_LOCALE);
if (self::DEFAULT_LOCALE !== \Locale::getDefault()) {
\Locale::setDefault(self::DEFAULT_LOCALE);
}
$formatter = new \NumberFormatter(\Locale::getDefault(), $style);
$formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $digits);
$formatter->setAttribute(\NumberFormatter::ROUNDING_MODE, $roundingMode);
Expand Down
5 changes: 0 additions & 5 deletions tests/Utils/DateUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class DateUtilsTest extends TestCase
protected function setUp(): void
{
\Locale::setDefault(FormatUtils::DEFAULT_LOCALE);
\setlocale(\LC_ALL, FormatUtils::DEFAULT_LOCALE);
}

public static function getCompletYears(): \Iterator
Expand Down Expand Up @@ -338,10 +337,6 @@ public function testSubByString(): void
#[DataProvider('getWeekdayNames')]
public function testWeekdayNames(string $name, int $index, string $firstDay = 'sunday'): void
{
if (!$this->updateLocale()) {
self::markTestSkipped('Unable to set locale to "fr_CH".');
}

$values = DateUtils::getWeekdays($firstDay);
self::assertArrayHasKey($index, $values);
self::assertSame($name, $values[$index]);
Expand Down

0 comments on commit 0d7f01a

Please sign in to comment.