Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b3446d2

Browse files
[Translation] make intl+icu format seamless by handling it in MessageCatalogue
1 parent 100f205 commit b3446d2

File tree

5 files changed

+68
-17
lines changed

5 files changed

+68
-17
lines changed

src/Symfony/Component/Translation/Catalogue/MergeOperation.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\Translation\Catalogue;
1313

14+
use Symfony\Component\Translation\Formatter\IntlFormatterInterface;
15+
1416
/**
1517
* Merge operation between two catalogues as follows:
1618
* all = source ∪ target = {x: x ∈ source ∨ x ∈ target}
@@ -32,10 +34,11 @@ protected function processDomain($domain)
3234
'new' => array(),
3335
'obsolete' => array(),
3436
);
37+
$intlDomain = $domain.IntlFormatterInterface::DOMAIN_SUFFIX;
3538

3639
foreach ($this->source->all($domain) as $id => $message) {
3740
$this->messages[$domain]['all'][$id] = $message;
38-
$this->result->add(array($id => $message), $domain);
41+
$this->result->add(array($id => $message), $this->source->defines($id, $intlDomain) ? $intlDomain : $domain);
3942
if (null !== $keyMetadata = $this->source->getMetadata($id, $domain)) {
4043
$this->result->setMetadata($id, $keyMetadata, $domain);
4144
}
@@ -45,7 +48,7 @@ protected function processDomain($domain)
4548
if (!$this->source->has($id, $domain)) {
4649
$this->messages[$domain]['all'][$id] = $message;
4750
$this->messages[$domain]['new'][$id] = $message;
48-
$this->result->add(array($id => $message), $domain);
51+
$this->result->add(array($id => $message), $this->target->defines($id, $intlDomain) ? $intlDomain : $domain);
4952
if (null !== $keyMetadata = $this->target->getMetadata($id, $domain)) {
5053
$this->result->setMetadata($id, $keyMetadata, $domain);
5154
}

src/Symfony/Component/Translation/Catalogue/TargetOperation.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\Translation\Catalogue;
1313

14+
use Symfony\Component\Translation\Formatter\IntlFormatterInterface;
15+
1416
/**
1517
* Target operation between two catalogues:
1618
* intersection = source ∩ target = {x: x ∈ source ∧ x ∈ target}
@@ -33,6 +35,7 @@ protected function processDomain($domain)
3335
'new' => array(),
3436
'obsolete' => array(),
3537
);
38+
$intlDomain = $domain.IntlFormatterInterface::DOMAIN_SUFFIX;
3639

3740
// For 'all' messages, the code can't be simplified as ``$this->messages[$domain]['all'] = $target->all($domain);``,
3841
// because doing so will drop messages like {x: x ∈ source ∧ x ∉ target.all ∧ x ∈ target.fallback}
@@ -46,7 +49,7 @@ protected function processDomain($domain)
4649
foreach ($this->source->all($domain) as $id => $message) {
4750
if ($this->target->has($id, $domain)) {
4851
$this->messages[$domain]['all'][$id] = $message;
49-
$this->result->add(array($id => $message), $domain);
52+
$this->result->add(array($id => $message), $this->target->defines($id, $intlDomain) ? $intlDomain : $domain);
5053
if (null !== $keyMetadata = $this->source->getMetadata($id, $domain)) {
5154
$this->result->setMetadata($id, $keyMetadata, $domain);
5255
}
@@ -59,7 +62,7 @@ protected function processDomain($domain)
5962
if (!$this->source->has($id, $domain)) {
6063
$this->messages[$domain]['all'][$id] = $message;
6164
$this->messages[$domain]['new'][$id] = $message;
62-
$this->result->add(array($id => $message), $domain);
65+
$this->result->add(array($id => $message), $this->target->defines($id, $intlDomain) ? $intlDomain : $domain);
6366
if (null !== $keyMetadata = $this->target->getMetadata($id, $domain)) {
6467
$this->result->setMetadata($id, $keyMetadata, $domain);
6568
}

src/Symfony/Component/Translation/Dumper/FileDumper.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Translation\Exception\InvalidArgumentException;
1515
use Symfony\Component\Translation\Exception\RuntimeException;
16+
use Symfony\Component\Translation\Formatter\IntlFormatterInterface;
1617
use Symfony\Component\Translation\MessageCatalogue;
1718

1819
/**
@@ -76,8 +77,21 @@ public function dump(MessageCatalogue $messages, $options = array())
7677
throw new RuntimeException(sprintf('Unable to create directory "%s".', $directory));
7778
}
7879
}
79-
// save file
80-
file_put_contents($fullpath, $this->formatCatalogue($messages, $domain, $options));
80+
81+
$intlDomain = $domain.IntlFormatterInterface::DOMAIN_SUFFIX;
82+
$intlMessages = $messages->all($intlDomain);
83+
84+
if ($intlMessages) {
85+
$intlPath = $options['path'].'/'.$this->getRelativePath($intlDomain, $messages->getLocale());
86+
file_put_contents($intlPath, $this->formatCatalogue($messages, $intlDomain, $options));
87+
88+
$messages->replace(array(), $intlDomain);
89+
}
90+
try {
91+
file_put_contents($fullpath, $this->formatCatalogue($messages, $domain, $options));
92+
} finally {
93+
$messages->replace($intlMessages, $intlDomain);
94+
}
8195
}
8296
}
8397

src/Symfony/Component/Translation/MessageCatalogue.php

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Config\Resource\ResourceInterface;
1515
use Symfony\Component\Translation\Exception\LogicException;
16+
use Symfony\Component\Translation\Formatter\IntlFormatterInterface;
1617

1718
/**
1819
* @author Fabien Potencier <[email protected]>
@@ -49,19 +50,41 @@ public function getLocale()
4950
*/
5051
public function getDomains()
5152
{
52-
return array_keys($this->messages);
53+
$domains = array();
54+
$suffixLength = \strlen(IntlFormatterInterface::DOMAIN_SUFFIX);
55+
56+
foreach ($this->messages as $domain => $messages) {
57+
if (\strlen($domain) > $suffixLength && false !== $i = strpos($domain, IntlFormatterInterface::DOMAIN_SUFFIX, -$suffixLength)) {
58+
$domain = substr($domain, 0, $i);
59+
}
60+
$domains[$domain] = $domain;
61+
}
62+
63+
return array_values($domains);
5364
}
5465

5566
/**
5667
* {@inheritdoc}
5768
*/
5869
public function all($domain = null)
5970
{
60-
if (null === $domain) {
61-
return $this->messages;
71+
if (null !== $domain) {
72+
return ($this->messages[$domain.IntlFormatterInterface::DOMAIN_SUFFIX] ?? array()) + ($this->messages[$domain] ?? array());
73+
}
74+
75+
$allMessages = array();
76+
$suffixLength = \strlen(IntlFormatterInterface::DOMAIN_SUFFIX);
77+
78+
foreach ($this->messages as $domain => $messages) {
79+
if (\strlen($domain) > $suffixLength && false !== $i = strpos($domain, IntlFormatterInterface::DOMAIN_SUFFIX, -$suffixLength)) {
80+
$domain = substr($domain, 0, $i);
81+
$allMessages[$domain] = $messages + ($allMessages[$domain] ?? array());
82+
} else {
83+
$allMessages[$domain] = ($allMessages[$domain] ?? array()) + $messages;
84+
}
6285
}
6386

64-
return isset($this->messages[$domain]) ? $this->messages[$domain] : array();
87+
return $allMessages;
6588
}
6689

6790
/**
@@ -77,7 +100,7 @@ public function set($id, $translation, $domain = 'messages')
77100
*/
78101
public function has($id, $domain = 'messages')
79102
{
80-
if (isset($this->messages[$domain][$id])) {
103+
if (isset($this->messages[$domain][$id]) || isset($this->messages[$domain.IntlFormatterInterface::DOMAIN_SUFFIX][$id])) {
81104
return true;
82105
}
83106

@@ -93,14 +116,18 @@ public function has($id, $domain = 'messages')
93116
*/
94117
public function defines($id, $domain = 'messages')
95118
{
96-
return isset($this->messages[$domain][$id]);
119+
return isset($this->messages[$domain][$id]) || isset($this->messages[$domain.IntlFormatterInterface::DOMAIN_SUFFIX][$id]);
97120
}
98121

99122
/**
100123
* {@inheritdoc}
101124
*/
102125
public function get($id, $domain = 'messages')
103126
{
127+
if (isset($this->messages[$domain.IntlFormatterInterface::DOMAIN_SUFFIX][$id])) {
128+
return $this->messages[$domain.IntlFormatterInterface::DOMAIN_SUFFIX][$id];
129+
}
130+
104131
if (isset($this->messages[$domain][$id])) {
105132
return $this->messages[$domain][$id];
106133
}
@@ -117,7 +144,7 @@ public function get($id, $domain = 'messages')
117144
*/
118145
public function replace($messages, $domain = 'messages')
119146
{
120-
$this->messages[$domain] = array();
147+
unset($this->messages[$domain], $this->messages[$domain.IntlFormatterInterface::DOMAIN_SUFFIX]);
121148

122149
$this->add($messages, $domain);
123150
}
@@ -144,6 +171,10 @@ public function addCatalogue(MessageCatalogueInterface $catalogue)
144171
}
145172

146173
foreach ($catalogue->all() as $domain => $messages) {
174+
if ($intlMessages = $catalogue->all($domain.IntlFormatterInterface::DOMAIN_SUFFIX)) {
175+
$this->add($intlMessages, $domain.IntlFormatterInterface::DOMAIN_SUFFIX);
176+
$messages = array_diff_key($messages, $intlMessages);
177+
}
147178
$this->add($messages, $domain);
148179
}
149180

src/Symfony/Component/Translation/Translator.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,11 +203,7 @@ public function trans($id, array $parameters = array(), $domain = null, $locale
203203
$id = (string) $id;
204204
$catalogue = $this->getCatalogue($locale);
205205
$locale = $catalogue->getLocale();
206-
$intlDomain = $this->hasIntlFormatter ? $domain.IntlFormatterInterface::DOMAIN_SUFFIX : null;
207206
while (true) {
208-
if (null !== $intlDomain && $catalogue->defines($id, $intlDomain)) {
209-
return $this->formatter->formatIntl($catalogue->get($id, $intlDomain), $locale, $parameters);
210-
}
211207
if ($catalogue->defines($id, $domain)) {
212208
break;
213209
}
@@ -219,6 +215,10 @@ public function trans($id, array $parameters = array(), $domain = null, $locale
219215
}
220216
}
221217

218+
if ($this->hasIntlFormatter && $catalogue->defines($id, $domain.IntlFormatterInterface::DOMAIN_SUFFIX)) {
219+
return $this->formatter->formatIntl($catalogue->get($id, $domain), $locale, $parameters);
220+
}
221+
222222
return $this->formatter->format($catalogue->get($id, $domain), $locale, $parameters);
223223
}
224224

0 commit comments

Comments
 (0)