Skip to content

Commit 26bfebc

Browse files
committed
Load English file for setLocale('auto') tests
1 parent 65c726e commit 26bfebc

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

src/Carbon/AbstractTranslator.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ public function setLocale($locale): void
351351

352352
usort($locales, static fn ($first, $second) => $getScore($second) <=> $getScore($first));
353353

354-
$locale = $locales[0];
354+
$locale = $locales[0] ?? 'en';
355355
}
356356

357357
if (isset($this->aliases[$locale])) {
@@ -362,8 +362,8 @@ public function setLocale($locale): void
362362
// and the tag contains a region (ex: en_CA), then
363363
// first load the macro (ex: en) to have a fallback
364364
if (
365-
!\in_array($locale, self::getInternallySupportedLocales(), true)
366-
&& str_contains($locale, '_')
365+
str_contains($locale, '_')
366+
&& !\in_array($locale, self::getInternallySupportedLocales(), true)
367367
&& $this->loadMessagesFromFile($macroLocale = preg_replace('/^([^_]+).*$/', '$1', $locale))
368368
) {
369369
parent::setLocale($macroLocale);

tests/CarbonImmutable/LocalizationTest.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Carbon\Language;
1919
use Carbon\Translator;
2020
use InvalidArgumentException;
21+
use LogicException;
2122
use PHPUnit\Framework\Attributes\DataProvider;
2223
use PHPUnit\Framework\Attributes\Group;
2324
use PHPUnit\Framework\Attributes\TestWith;
@@ -69,15 +70,21 @@ public function testSetLocaleToAuto()
6970

7071
$this->setLocaleOrSkip('ar_AE.UTF-8', 'ar_AE.utf8', 'ar_AE', 'ar');
7172

72-
rename(__DIR__.'/../../src/Carbon/Lang/ar_AE.php', __DIR__.'/../../src/Carbon/Lang/disabled_ar_AE.php');
73-
/** @var Translator $translator */
74-
$translator = Carbon::getTranslator();
75-
$translator->resetMessages();
76-
Carbon::setLocale('auto');
77-
$locale = Carbon::getLocale();
78-
$diff = Carbon::now()->subSeconds(2)->diffForHumans();
79-
setlocale(LC_ALL, $currentLocale);
80-
rename(__DIR__.'/../../src/Carbon/Lang/disabled_ar_AE.php', __DIR__.'/../../src/Carbon/Lang/ar_AE.php');
73+
if (!rename(__DIR__.'/../../src/Carbon/Lang/ar_AE.php', __DIR__.'/../../src/Carbon/Lang/disabled_ar_AE.php')) {
74+
throw new LogicException('Unable to move ar_AE.php for the test');
75+
}
76+
77+
try {
78+
/** @var Translator $translator */
79+
$translator = Carbon::getTranslator();
80+
$translator->resetMessages();
81+
Carbon::setLocale('auto');
82+
$locale = Carbon::getLocale();
83+
$diff = Carbon::now()->subSeconds(2)->diffForHumans();
84+
setlocale(LC_ALL, $currentLocale);
85+
} finally {
86+
rename(__DIR__.'/../../src/Carbon/Lang/disabled_ar_AE.php', __DIR__.'/../../src/Carbon/Lang/ar_AE.php');
87+
}
8188

8289
$this->assertStringStartsWith('ar', $locale);
8390
$this->assertSame('منذ ثانيتين', $diff);
@@ -118,6 +125,7 @@ public function testSetLocaleToAuto()
118125
$translator->setDirectories([$directory]);
119126

120127
$files = [
128+
'en',
121129
'zh_Hans',
122130
'zh',
123131
'fr',

0 commit comments

Comments
 (0)