Skip to content

Commit bbdcef1

Browse files
committed
use site locale
1 parent 68af710 commit bbdcef1

6 files changed

Lines changed: 85 additions & 14 deletions

File tree

src/Modifiers/IsEndOfWeek.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,26 @@
33
namespace TransformStudios\Events\Modifiers;
44

55
use Carbon\CarbonImmutable;
6+
use Statamic\Facades\Site;
67
use Statamic\Modifiers\Modifier;
78

89
class IsEndOfWeek extends Modifier
910
{
1011
public function index($value, $params, $context)
1112
{
13+
/*
14+
have to do this because Statamic sets the Carbon locale
15+
to the `lang` of the site, instead of the `locale`
16+
*/
17+
$currentLocale = CarbonImmutable::getLocale();
18+
CarbonImmutable::setLocale(Site::current()->locale());
19+
1220
$date = CarbonImmutable::parse($value);
1321

14-
$date->isSameDay($date->locale(CarbonImmutable::getLocale())->startOfWeek());
22+
$isStartOfWeek = $date->dayOfWeek == now()->endOfWeek()->dayOfWeek;
23+
24+
CarbonImmutable::setLocale($currentLocale);
1525

16-
return $date->dayOfWeek == now()->endOfWeek()->dayOfWeek;
26+
return $isStartOfWeek;
1727
}
1828
}

src/Modifiers/IsStartOfWeek.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,26 @@
33
namespace TransformStudios\Events\Modifiers;
44

55
use Carbon\CarbonImmutable;
6+
use Statamic\Facades\Site;
67
use Statamic\Modifiers\Modifier;
78

89
class IsStartOfWeek extends Modifier
910
{
1011
public function index($value, $params, $context)
1112
{
13+
/*
14+
have to do this because Statamic sets the Carbon locale
15+
to the `lang` of the site, instead of the `locale`
16+
*/
17+
$currentLocale = CarbonImmutable::getLocale();
18+
CarbonImmutable::setLocale(Site::current()->locale());
19+
1220
$date = CarbonImmutable::parse($value);
1321

14-
$date->isSameDay($date->locale(CarbonImmutable::getLocale())->startOfWeek());
22+
$isStartOfWeek = $date->dayOfWeek == now()->startOfWeek()->dayOfWeek;
23+
24+
CarbonImmutable::setLocale($currentLocale);
1525

16-
return $date->dayOfWeek == now()->startOfWeek()->dayOfWeek;
26+
return $isStartOfWeek;
1727
}
1828
}

src/Tags/Events.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Statamic\Entries\Entry;
1414
use Statamic\Entries\EntryCollection;
1515
use Statamic\Facades\Compare;
16+
use Statamic\Facades\Site;
1617
use Statamic\Support\Arr;
1718
use Statamic\Support\Str;
1819
use Statamic\Tags\Concerns\OutputsItems;
@@ -50,12 +51,23 @@ public function calendar(): Collection
5051

5152
public function daysOfWeek(): Collection
5253
{
53-
return collect(CarbonPeriod::dates(now()->startOfWeek(), now()->endOfWeek()))
54+
/*
55+
have to do this because Statamic sets the Carbon locale
56+
to the `lang` of the site, instead of the `locale`
57+
*/
58+
$currentLocale = Carbon::getLocale();
59+
Carbon::setLocale(Site::current()->locale());
60+
61+
$days = collect(CarbonPeriod::dates(now()->startOfWeek(), now()->endOfWeek()))
5462
->map(fn (Carbon $date) => [
5563
'short' => $date->format('D')[0],
5664
'medium' => $date->format('D'),
5765
'long' => $date->format('l'),
5866
]);
67+
68+
Carbon::setLocale($currentLocale);
69+
70+
return $days;
5971
}
6072

6173
public function downloadLink(): string
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
use Carbon\Carbon;
4+
use Statamic\Facades\Site as SiteFacade;
5+
use Statamic\Modifiers\Modify;
6+
use Statamic\Sites\Site;
7+
8+
it('uses current sites locale to determine start of week', function () {
9+
SiteFacade::shouldReceive('current')
10+
->andReturn(new Site('default', [
11+
'name' => 'Laravel',
12+
'url' => '/',
13+
'locale' => 'en_US',
14+
'lang' => 'en',
15+
], true));
16+
17+
Carbon::setTestNow('2026-3-25 12:00pm');
18+
19+
$modified = modify('2026-3-22');
20+
expect($modified)->toBe(true);
21+
});
22+
23+
function modify(string $value)
24+
{
25+
return Modify::value($value)->isStartOfWeek()->fetch();
26+
}

tests/Tags/EventsTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use Illuminate\Support\Carbon;
66
use Statamic\Facades\Cascade;
77
use Statamic\Facades\Entry;
8+
use Statamic\Facades\Site as SiteFacade;
9+
use Statamic\Sites\Site;
810
use Statamic\Support\Arr;
911
use TransformStudios\Events\Tags\Events;
1012

@@ -387,3 +389,23 @@
387389

388390
expect($this->tag->today())->toHaveCount(0);
389391
});
392+
393+
it('sets the correct short form of the days of week', function () {
394+
expect($this->tag->daysOfWeek())->pluck('short')->toBe(['M', 'Tu', 'W', 'Th', 'F', 'Sa', 'Su']);
395+
});
396+
397+
it('uses the current site locale to get days of week', function () {
398+
SiteFacade::shouldReceive('current')
399+
->andReturn(new Site('default', [
400+
'name' => 'Laravel',
401+
'url' => '/',
402+
'locale' => 'en_US',
403+
'lang' => 'en',
404+
], true));
405+
406+
expect($this->tag->daysOfWeek())->first()->toBe([
407+
'short' => 'S',
408+
'medium' => 'Sun',
409+
'long' => 'Sunday',
410+
]);
411+
});

tests/TestCase.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,6 @@ abstract class TestCase extends AddonTestCase
2828

2929
protected Blueprint $blueprint;
3030

31-
protected function setUp(): void
32-
{
33-
parent::setUp();
34-
35-
if (! file_exists($this->fakeStacheDirectory)) {
36-
mkdir($this->fakeStacheDirectory, 0777, true);
37-
}
38-
}
39-
4031
protected function getEnvironmentSetUp($app)
4132
{
4233
parent::getEnvironmentSetUp($app);

0 commit comments

Comments
 (0)