Skip to content

Commit 1dab1c7

Browse files
authored
feat(intl): add current_locale util (#1643)
1 parent b3acd5f commit 1dab1c7

File tree

9 files changed

+49
-15
lines changed

9 files changed

+49
-15
lines changed

packages/datetime/src/DateTimeConvenienceMethods.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -922,7 +922,7 @@ public function isEndOfWeek(): bool
922922
*
923923
* @param null|FormatPattern|string $pattern Optional custom format pattern for the date and time. If null, uses a default pattern.
924924
* @param null|Timezone $timezone Optional timezone for formatting. If null, uses the current timezone.
925-
* @param null|Locale $locale Optional locale for formatting. If null, uses the system's default locale.
925+
* @param null|Locale $locale Optional locale for formatting. If null, uses the configured locale or system default as fallback.
926926
*
927927
* @return string The formatted date and time string, according to the specified pattern, timezone, and locale.
928928
*
@@ -992,7 +992,7 @@ public function toRfc3339(?SecondsStyle $secondsStyle = null, bool $useZ = false
992992
* @param null|DateStyle $dateStyle Optional style for the date portion of the output. If null, a default style is used.
993993
* @param null|TimeStyle $timeStyle Optional style for the time portion of the output. If null, a default style is used.
994994
* @param null|Timezone $timezone Optional timezone for formatting. If null, uses the current timezone.
995-
* @param null|Locale $locale Optional locale for formatting. If null, uses the system's default locale.
995+
* @param null|Locale $locale Optional locale for formatting. If null, uses the configured locale or system default as fallback.
996996
*
997997
* @return string The string representation of the date and time, formatted according to the specified styles, timezone, and locale.
998998
*

packages/datetime/src/functions.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
use function hrtime;
1818
use function microtime;
19+
use function Tempest\Intl\current_locale;
1920

2021
use const Tempest\DateTime\NANOSECONDS_PER_SECOND;
2122

@@ -94,7 +95,7 @@ function create_intl_date_formatter(
9495

9596
$dateStyle ??= DateStyle::default();
9697
$timeStyle ??= TimeStyle::default();
97-
$locale ??= Locale::default();
98+
$locale ??= current_locale();
9899
$timezone ??= Timezone::default();
99100

100101
return new IntlDateFormatter(

packages/intl/src/functions.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Countable;
66
use Stringable;
7+
use Tempest\Container\GenericContainer;
78
use Tempest\Intl\Locale;
89
use Tempest\Intl\Pluralizer\Pluralizer;
910
use Tempest\Intl\Translator;
@@ -57,3 +58,13 @@ function pluralize_last_word(Stringable|string $value, int|array|Countable $coun
5758
{
5859
return get(Pluralizer::class)->pluralizeLastWord($value, $count);
5960
}
61+
62+
/**
63+
* Returns the configured locale from the container, or the system default if not configured.
64+
*/
65+
function current_locale(): Locale
66+
{
67+
return GenericContainer::instance()?->has(IntlConfig::class)
68+
? GenericContainer::instance()->get(IntlConfig::class)->currentLocale
69+
: Locale::default();
70+
}

packages/intl/tests/FormatterTest.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function test_format_datetime_function_and_parameters(): void
5656
$this->markTestSkipped('`tempest/datetime` is needed for this test.');
5757
}
5858

59-
$formatter = new MessageFormatter([new DateTimeFunction()]);
59+
$formatter = new MessageFormatter([$this->createDateTimeFunction()]);
6060

6161
$value = $formatter->format(<<<'TXT'
6262
Today is {$today :datetime pattern=|yyyy/MM/dd|}.
@@ -367,7 +367,7 @@ public function test_multiple_selectors(): void
367367
{
368368
$formatter = new MessageFormatter([
369369
$this->createNumberFunction(),
370-
new DateTimeFunction(),
370+
$this->createDateTimeFunction(),
371371
]);
372372

373373
$value = $formatter->format(<<<'TXT'
@@ -417,4 +417,11 @@ private function createNumberFunction(): NumberFunction
417417
new IntlConfig(Locale::default(), Locale::default()),
418418
);
419419
}
420+
421+
private function createDateTimeFunction(): DateTimeFunction
422+
{
423+
return new DateTimeFunction(
424+
new IntlConfig(Locale::default(), Locale::default()),
425+
);
426+
}
420427
}

packages/intl/tests/GenericTranslatorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ protected function setUp(): void
3737
formatter: new MessageFormatter([
3838
new StringFunction(),
3939
new NumberFunction($this->config),
40-
new DateTimeFunction(),
40+
new DateTimeFunction($this->config),
4141
]),
4242
);
4343
}

packages/intl/tests/LocaleTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use function locale_get_default;
1313
use function locale_set_default;
14+
use function Tempest\Intl\current_locale;
1415
use function Tempest\Support\Str\replace_every;
1516

1617
final class LocaleTest extends TestCase
@@ -252,4 +253,13 @@ public function test_it_does_not_returns_the_region(Locale $locale): void
252253
$this->assertNull($locale->getRegion());
253254
$this->assertNull($locale->getDisplayRegion());
254255
}
256+
257+
public function test_current_locale(): void
258+
{
259+
foreach (Locale::cases() as $locale) {
260+
locale_set_default($locale->value);
261+
262+
$this->assertSame($locale, current_locale());
263+
}
264+
}
255265
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
use Tempest\Intl\IntlConfig;
4+
use Tempest\Intl\Locale;
5+
6+
return new IntlConfig(
7+
currentLocale: Locale::ENGLISH_UNITED_STATES,
8+
fallbackLocale: Locale::ENGLISH_UNITED_STATES,
9+
);

tests/Integration/Intl/FunctionsTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,9 @@ public function test_pluralize_last_word(): void
4545
$this->assertEquals('Multiple Migrations', Intl\pluralize_last_word('Multiple Migrations'));
4646
$this->assertEquals('Multiple Aircraft', Intl\pluralize_last_word('Multiple Aircraft'));
4747
}
48+
49+
public function test_current_locale(): void
50+
{
51+
$this->assertSame(Intl\Locale::ENGLISH_UNITED_STATES, Intl\current_locale());
52+
}
4853
}

tests/Integration/Intl/TranslatorTest.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ final class TranslatorTest extends FrameworkIntegrationTestCase
1919
{
2020
protected function setUp(): void
2121
{
22-
ini_set('intl.default_locale', 'en-US'); // @mago-expect lint:no-ini-set
23-
2422
parent::setUp();
2523

2624
$config = $this->container->get(IntlConfig::class);
@@ -46,13 +44,6 @@ public function test_function(): void
4644
$this->assertSame('Passer à la caisse', translate_locale(Locale::FRENCH, 'cart.checkout'));
4745
}
4846

49-
public function test_default_locale(): void
50-
{
51-
$config = $this->container->get(IntlConfig::class);
52-
53-
$this->assertSame(Locale::default(), $config->currentLocale);
54-
}
55-
5647
public function test_event_miss(): void
5748
{
5849
/** @var TranslationMiss|null $received */

0 commit comments

Comments
 (0)