Skip to content

Commit

Permalink
Merge pull request #257 from mimmi20/feature-exotic-locales
Browse files Browse the repository at this point in the history
Add support for exotic locales
  • Loading branch information
Slamdunk authored Jan 9, 2024
2 parents bb1b612 + 9633b46 commit b3c6575
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/View/Helper/FormDateTimeSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use function array_key_exists;
use function is_numeric;
use function preg_match_all;
use function preg_split;
use function rtrim;
use function sprintf;
Expand Down Expand Up @@ -203,7 +204,21 @@ public function getPattern(): string
*/
protected function parsePattern(bool $renderDelimiters = true): array
{
$pattern = $this->getPattern();
$pattern = $this->getPattern();

// are there any non-latin characters in the pattern?
$count = preg_match_all('/[^\p{Latin}\s\-,.:\/\\\'\[\]\(\)]+/u', $pattern, $matches);

if ($count) {
// put single quotes around these characters
foreach ($matches[0] as $match) {
$pattern = str_replace($match, "'$match'", $pattern);
}

// remove double quotes, if there were already quotes around them
$pattern = str_replace("''", "'", $pattern);
}

$pregResult = preg_split(
"/([ \-,.:\/]*'.*?'[ \-,.:\/]*)|([ \-,.:\/]+)/",
$pattern,
Expand Down
15 changes: 15 additions & 0 deletions test/View/Helper/FormDateTimeSelectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -453,4 +453,19 @@ public function testShortTimeFormatDoesNotProvideSecondPart(): void
self::assertStringContainsString('<select name="minute">', $markup);
self::assertStringNotContainsString('<select name="second">', $markup);
}

public function testRendersDatesWithZhHansHKLocaleDatePattern(): void
{
$this->helper->setLocale('zh_Hans_HK');
$this->helper->setDateType(IntlDateFormatter::LONG);
self::assertSame('y年M月d日 z ah:mm:ss', $this->helper->getPattern());

$element = new DateTimeSelect('foo');
$element->setMinYear(2022);
$element->setMaxYear(2027);

$markup = $this->helper->render($element);

self::assertStringContainsString('<select name="year">', $markup);
}
}

0 comments on commit b3c6575

Please sign in to comment.