Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit326dd08

Browse files
[Translation] allow using the ICU message format using domains with the "+intl-icu" suffix
1 parent8c24c35 commit326dd08

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
<tagname="monolog.logger"channel="translation" />
3030
</service>
3131

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

4236
<serviceid="translation.loader.php"class="Symfony\Component\Translation\Loader\PhpFileLoader">
4337
<tagname="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 <fabien@symfony.com>
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+
namespaceSymfony\Component\Translation\Formatter;
13+
14+
useSymfony\Component\Translation\Exception\InvalidArgumentException;
15+
useSymfony\Component\Translation\Exception\LogicException;
16+
17+
/**
18+
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
19+
* @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
20+
*/
21+
class IntlFormatterimplements IntlFormatterInterface
22+
{
23+
private$hasMessageFormatter;
24+
private$cache =array();
25+
26+
/**
27+
* {@inheritdoc}
28+
*/
29+
publicfunctionformatIntl(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+
thrownewLogicException('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+
thrownewInvalidArgumentException(sprintf('Invalid message format (error #%d): %s.',intl_get_error_code(),intl_get_error_message()),0,$e);
39+
}
40+
}
41+
42+
foreach ($parametersas$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+
thrownewInvalidArgumentException(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 <fabien@symfony.com>
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+
namespaceSymfony\Component\Translation\Formatter;
13+
14+
/**
15+
* Formats ICU message patterns.
16+
*
17+
* @author Nicolas Grekas <p@tchwork.com>
18+
*/
19+
interface IntlFormatterInterface
20+
{
21+
constDOMAIN_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+
publicfunctionformatIntl(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 <a.aitboudad@gmail.com>
2121
*/
22-
class MessageFormatterimplements MessageFormatterInterface, ChoiceMessageFormatterInterface
22+
class MessageFormatterimplements 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-
publicfunction__construct($translator =null)
30+
publicfunction__construct($translator =null,IntlFormatterInterface$intlFormatter =null)
3031
{
3132
if ($translatorinstanceof MessageSelector) {
3233
$translator =newIdentityTranslator($translator);
@@ -35,6 +36,7 @@ public function __construct($translator = null)
3536
}
3637

3738
$this->translator =$translator ??newIdentityTranslator();
39+
$this->intlFormatter =$intlFormatter ??newIntlFormatter();
3840
}
3941

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

54+
/**
55+
* {@inheritdoc}
56+
*/
57+
publicfunctionformatIntl(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)

[8]ページ先頭

©2009-2025 Movatter.jp