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

Skip to content

Commit d95cc4d

Browse files
[Translation] allow using the ICU message format using domains with the "+intl-icu" suffix
1 parent 8c24c35 commit d95cc4d

File tree

13 files changed

+158
-355
lines changed

13 files changed

+158
-355
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ private function addTranslatorSection(ArrayNodeDefinition $rootNode)
699699
->defaultValue(array('en'))
700700
->end()
701701
->booleanNode('logging')->defaultValue(false)->end()
702-
->scalarNode('formatter')->defaultValue(class_exists(\MessageFormatter::class) ? 'translator.formatter.default' : 'translator.formatter.symfony')->end()
702+
->scalarNode('formatter')->defaultValue('translator.formatter.default')->end()
703703
->scalarNode('default_path')
704704
->info('The default path used to load translations')
705705
->defaultValue('%kernel.project_dir%/translations')

src/Symfony/Bundle/FrameworkBundle/Resources/config/translation.xml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,9 @@
2929
<tag name="monolog.logger" channel="translation" />
3030
</service>
3131

32-
<service id="translator.formatter.symfony" class="Symfony\Component\Translation\Formatter\MessageFormatter">
32+
<service id="translator.formatter.default" class="Symfony\Component\Translation\Formatter\MessageFormatter">
3333
<argument type="service" id="identity_translator" />
3434
</service>
35-
<service id="translator.formatter.intl" class="Symfony\Component\Translation\Formatter\IntlMessageFormatter" public="false" />
36-
<service id="translator.formatter.fallback" class="Symfony\Component\Translation\Formatter\FallbackFormatter" public="false">
37-
<argument type="service" id="translator.formatter.intl" />
38-
<argument type="service" id="translator.formatter.symfony" />
39-
</service>
40-
<service id="translator.formatter.default" alias="translator.formatter.fallback" />
4135

4236
<service id="translation.loader.php" class="Symfony\Component\Translation\Loader\PhpFileLoader">
4337
<tag name="translation.loader" alias="php" />

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ protected static function getBundleDefaultConfig()
189189
'enabled' => !class_exists(FullStack::class),
190190
'fallbacks' => array('en'),
191191
'logging' => false,
192-
'formatter' => \class_exists('MessageFormatter') ? 'translator.formatter.default' : 'translator.formatter.symfony',
192+
'formatter' => 'translator.formatter.default',
193193
'paths' => array(),
194194
'default_path' => '%kernel.project_dir%/translations',
195195
),

src/Symfony/Component/Translation/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ CHANGELOG
55
-----
66

77
* Started using ICU parent locales as fallback locales.
8+
* allow using the ICU message format using domains with the "+intl-icu" suffix
89
* deprecated `Translator::transChoice()` in favor of using `Translator::trans()` with a `%count%` parameter
910
* deprecated `TranslatorInterface` in favor of `Symfony\Contracts\Translation\TranslatorInterface`
1011
* deprecated `MessageSelector`, `Interval` and `PluralizationRules`; use `IdentityTranslator` instead
11-
* Added `IntlMessageFormatter` and `FallbackMessageFormatter`
12+
* Added `IntlFormatter` and `IntlFormatterInterface`
1213
* added support for multiple files and directories in `XliffLintCommand`
1314
* Marked `Translator::getFallbackLocales()` and `TranslationDataCollector::getFallbackLocales()` as internal
1415

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

Lines changed: 0 additions & 77 deletions
This file was deleted.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Translation\Formatter;
13+
14+
use Symfony\Component\Translation\Exception\InvalidArgumentException;
15+
use Symfony\Component\Translation\Exception\LogicException;
16+
17+
/**
18+
* @author Guilherme Blanco <[email protected]>
19+
* @author Abdellatif Ait boudad <[email protected]>
20+
*/
21+
class IntlFormatter implements IntlFormatterInterface
22+
{
23+
private $hasMessageFormatter;
24+
private $cache = array();
25+
26+
/**
27+
* {@inheritdoc}
28+
*/
29+
public function formatIntl(string $message, string $locale, array $parameters = array()): string
30+
{
31+
if (!$formatter = $this->cache[$locale][$message] ?? null) {
32+
if (!($this->hasMessageFormatter ?? $this->hasMessageFormatter = class_exists(\MessageFormatter::class))) {
33+
throw new LogicException('Cannot parse message translation: please install the "intl" PHP extension or the "symfony/polyfill-intl-messageformatter" package.');
34+
}
35+
try {
36+
$this->cache[$locale][$message] = $formatter = new \MessageFormatter($locale, $message);
37+
} catch (\IntlException $e) {
38+
throw new InvalidArgumentException(sprintf('Invalid message format (error #%d): %s.', intl_get_error_code(), intl_get_error_message()), 0, $e);
39+
}
40+
}
41+
42+
foreach ($parameters as $key => $value) {
43+
if (\in_array($key[0] ?? null, array('%', '{'), true)) {
44+
unset($parameters[$key]);
45+
$parameters[trim($key, '%{ }')] = $value;
46+
}
47+
}
48+
49+
if (false === $message = $formatter->format($parameters)) {
50+
throw new InvalidArgumentException(sprintf('Unable to format message (error #%s): %s.', $formatter->getErrorCode(), $formatter->getErrorMessage()));
51+
}
52+
53+
return $message;
54+
}
55+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Translation\Formatter;
13+
14+
/**
15+
* Formats ICU message patterns.
16+
*
17+
* @author Nicolas Grekas <[email protected]>
18+
*/
19+
interface IntlFormatterInterface
20+
{
21+
const DOMAIN_SUFFIX = '+intl-icu';
22+
23+
/**
24+
* Formats a localized message using rules defined by ICU MessageFormat.
25+
*
26+
* @see http://icu-project.org/apiref/icu4c/classMessageFormat.html#details
27+
*/
28+
public function formatIntl(string $message, string $locale, array $parameters = array()): string;
29+
}

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

Lines changed: 0 additions & 40 deletions
This file was deleted.

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@
1919
/**
2020
* @author Abdellatif Ait boudad <[email protected]>
2121
*/
22-
class MessageFormatter implements MessageFormatterInterface, ChoiceMessageFormatterInterface
22+
class MessageFormatter implements MessageFormatterInterface, IntlFormatterInterface, ChoiceMessageFormatterInterface
2323
{
2424
private $translator;
25+
private $intlFormatter;
2526

2627
/**
2728
* @param TranslatorInterface|null $translator An identity translator to use as selector for pluralization
2829
*/
29-
public function __construct($translator = null)
30+
public function __construct($translator = null, IntlFormatterInterface $intlFormatter = null)
3031
{
3132
if ($translator instanceof MessageSelector) {
3233
$translator = new IdentityTranslator($translator);
@@ -35,6 +36,7 @@ public function __construct($translator = null)
3536
}
3637

3738
$this->translator = $translator ?? new IdentityTranslator();
39+
$this->intlFormatter = $intlFormatter ?? new IntlFormatter();
3840
}
3941

4042
/**
@@ -49,6 +51,14 @@ public function format($message, $locale, array $parameters = array())
4951
return strtr($message, $parameters);
5052
}
5153

54+
/**
55+
* {@inheritdoc}
56+
*/
57+
public function formatIntl(string $message, string $locale, array $parameters = array()): string
58+
{
59+
return $this->intlFormatter->formatIntl($message, $locale, $parameters);
60+
}
61+
5262
/**
5363
* {@inheritdoc}
5464
*

0 commit comments

Comments
 (0)