8000 Handle select · symfony/symfony@bf9ae82 · GitHub
[go: up one dir, main page]

Skip to content

Commit bf9ae82

Browse files
committed
Handle select
1 parent 0d5e3cb commit bf9ae82

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

src/Symfony/Component/Translation/Formatter/IntlSimpleMessageFormatter.php

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class IntlSimpleMessageFormatter implements MessageFormatterInterface
2626
public function format($message, $locale, array $parameters = array())
2727
{
2828
$message = $this->handlePlural($message, $parameters);
29+
$message = $this->handleSelect($message, $parameters);
2930
$message = $this->replaceParameters($message, $parameters);
3031

3132
return $message;
@@ -34,11 +35,12 @@ public function format($message, $locale, array $parameters = array())
3435
private function handlePlural(string $message, array $parameters): string
3536
{
3637
$lookupMap = [0 => 'zero', 1 => 'one', 2 => 'two', 3 => 'few'];
37-
//{0, plural, =0{no cat} =1{a cat} other{# cats}}
38+
3839
foreach ($parameters as $key => $value) {
3940
$regex = '|{ ?'.$key.', plural,(.*)|sm';
4041
if (preg_match($regex, $message, $match)) {
41-
$block = $this->findBlock($match[1]);
42+
$blockContent = $this->findBlock($match[1]);
43+
$fullBlock = substr($match[0], 0, strpos($match[0], $blockContent) + strlen($blockContent)+1);
4244

4345
$lookup = array();
4446
if (is_int($value)) {
@@ -52,15 +54,44 @@ private function handlePlural(string $message, array $parameters): string
5254
$lookup[] = 'other';
5355

5456
foreach ($lookup as $l) {
55-
if (preg_match('|'.$l.' ?{(.*)|sm', $block, $blockMatch)) {
57+
if (preg_match('|'.$l.' ?{(.*)|sm', $blockContent, $blockMatch)) {
58+
$result = $this->findBlock($blockMatch[1]);
59+
$blockReplacement = str_replace('#', $value, $result);
60+
61+
return str_replace($fullBlock, $blockReplacement, $message);
62+
}
63+
}
64+
}
65+
}
66+
67+
return $message;
68+
}
69+
70+
private function handleSelect(string $message, array $parameters): string
71+
{
72+
$regex = '|{ ?([a-zA-Z]+), select,(.*)|sm';
73+
if (preg_match($regex, $message, $match)) {
74+
$blockContent = $this->findBlock($match[2]);
75+
$fullBlock = substr($match[0], 0, strpos($match[0], $blockContent) + strlen($blockContent)+1);
76+
foreach ($parameters as $key => $value) {
77+
if ($match[1] === $key) {
78+
if (preg_match('|'.$value.' ?{(.*)|sm', $blockContent, $blockMatch)) {
5679
$result = $this->findBlock($blockMatch[1]);
5780

58-
return str_replace('#', $value, $result);
81+
return str_replace($fullBlock, $result, $message);
5982
}
6083
}
6184
}
85+
86+
// If no match
87+
if (preg_match('|other ?{(.*)|sm', $blockContent, $blockMatch)) {
88+
$result = $this->findBlock($blockMatch[1]);
89+
90+
return str_replace($fullBlock, $result, $message);
91+
}
6292
}
6393

94+
6495
return $message;
6596
}
6697

src/Symfony/Component/Translation/Tests/Formatter/IntlSimpleMessageFormatterTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,18 @@ public function getTestStrings()
3939
yield array($apples, 'Foo, there are no apples', array('COUNT' => 0, 'name' => 'Foo'));
4040
yield array($apples, 'Foo, there is one apple', array('COUNT' => 1, 'name' => 'Foo'));
4141
yield array($apples, 'Foo, there are 2 apples', array('COUNT' => 2, 'name' => 'Foo'));
42+
yield array('Hello {name}. There are {COUNT, plural, zero{no apples} other{some apples}} in the basket', 'Hello Foo. There are no apples in the basket', array('COUNT' => 0, 'name' => 'Foo'));
43+
yield array('Hello {name}. There are {COUNT, plural, zero{no apples} other{some apples}} in the basket', 'Hello Foo. There are some apples in the basket', array('COUNT' => 3, 'name' => 'Foo'));
44+
45+
// Test select
46+
$gender = 'I think { GENDER, select,
47+
male {he}
48+
female {she}
49+
other {they}
50+
} liked this.';
51+
yield array($gender, 'I think he liked this.', array('GENDER' => 'male'));
52+
yield array($gender, 'I think she liked this.', array('GENDER' => 'female'));
53+
yield array($gender, 'I think they liked this.', array());
54+
4255
}
4356
}

0 commit comments

Comments
 (0)
0