8000 [Translation] allow using the ICU message format using domains with the "+intl-icu" suffix by nicolas-grekas · Pull Request #28952 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Translation] allow using the ICU message format using domains with the "+intl-icu" suffix #28952

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ private function addTranslatorSection(ArrayNodeDefinition $rootNode)
->defaultValue(array('en'))
->end()
->booleanNode('logging')->defaultValue(false)->end()
->scalarNode('formatter')->defaultValue(class_exists(\MessageFormatter::class) ? 'translator.formatter.default' : 'translator.formatter.symfony')->end()
->scalarNode('formatter')->defaultValue('translator.formatter.default')->end()
->scalarNode('default_path')
->info('The default path used to load translations')
->defaultValue('%kernel.project_dir%/translations')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,9 @@
<tag name="monolog.logger" channel="translation" />
</service>

<service id="translator.formatter.symfony" class="Symfony\Component\Translation\Formatter\MessageFormatter">
<service id="translator.formatter.default" class="Symfony\Component\Translation\Formatter\MessageFormatter">
<argument type="service" id="identity_translator" />
</service>
<service id="translator.formatter.intl" class="Symfony\Component\Translation\Formatter\IntlMessageFormatter" public="false" />
<service id="translator.formatter.fallback" class="Symfony\Component\Translation\Formatter\FallbackFormatter" public="false">
<argument type="service" id="translator.formatter.intl" />
<argument type="service" id="translator.formatter.symfony" />
</service>
<service id="translator.formatter.default" alias="translator.formatter.fallback" />

<service id="translation.loader.php" class="Symfony\Component\Translation\Loader\PhpFileLoader">
<tag name="translation.loader" alias="php" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ protected static function getBundleDefaultConfig()
'enabled' => !class_exists(FullStack::class),
'fallbacks' => array('en'),
'logging' => false,
'formatter' => \class_exists('MessageFormatter') ? 'translator.formatter.default' : 'translator.formatter.symfony',
'formatter' => 'translator.formatter.default',
'paths' => array(),
'default_path' => '%kernel.project_dir%/translations',
),
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Component/Translation/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ CHANGELOG
-----

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

Expand Down
77 changes: 0 additions & 77 deletions src/Symfony/Component/Translation/Formatter/FallbackFormatter.php

This file was deleted.

55 changes: 55 additions & 0 deletions src/Symfony/Component/Translation/Formatter/IntlFormatter.php
Original file line number Diff line 8000 number Diff line change
@@ -0,0 +1,55 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Translation\Formatter;

use Symfony\Component\Translation\Exception\InvalidArgumentException;
use Symfony\Component\Translation\Exception\LogicException;

/**
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
*/
class IntlFormatter implements IntlFormatterInterface
{
private $hasMessageFormatter;
private $cache = array();

/**
* {@inheritdoc}
*/
public function formatIntl(string $message, string $locale, array $parameters = array()): string
{
if (!$formatter = $this->cache[$locale][$message] ?? null) {
if (!($this->hasMessageFormatter ?? $this->hasMessageFormatter = class_exists(\MessageFormatter::class))) {
throw new LogicException('Cannot parse message translation: please install the "intl" PHP extension or the "symfony/polyfill-intl-messageformatter" package.');
}
try {
$this->cache[$locale][$message] = $formatter = new \MessageFormatter($locale, $message);
} catch (\IntlException $e) {
throw new InvalidArgumentException(sprintf('Invalid message format (error #%d): %s.', intl_get_error_code(), intl_get_error_message()), 0, $e);
}
}

foreach ($parameters as $key => $value) {
if (\in_array($key[0] ?? null, array('%', '{'), true)) {
unset($parameters[$key]);
$parameters[trim($key, '%{ }')] = $value;
}
}

if (false === $message = $formatter->format($parameters)) {
throw new InvalidArgumentException(sprintf('Unable to format message (error #%s): %s.', $formatter->getErrorCode(), $formatter->getErrorMessage()));
}

return $message;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Translation\Formatter;

/**
* Formats ICU message patterns.
*
* @author Nicolas Grekas <p@tchwork.com>
*/
interface IntlFormatterInterface
{
const DOMAIN_SUFFIX = '+intl-icu';

/**
* Formats a localized message using rules defined by ICU MessageFormat.
*
* @see http://icu-project.org/apiref/icu4c/classMessageFormat.html#details
*/
public function formatIntl(string $message, string $locale, array $parameters = array()): string;
}

This file was deleted.

14 changes: 12 additions & 2 deletions src/Symfony/Component/Translation/Formatter/MessageFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@
/**
* @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
*/
class MessageFormatter implements MessageFormatterInterface, ChoiceMessageFormatterInterface
class MessageFormatter implements MessageFormatterInterface, IntlFormatterInterface, ChoiceMessageFormatterInterface
{
private $translator;
private $intlFormatter;

/**
* @param TranslatorInterface|null $translator An identity translator to use as selector for pluralization
*/
public function __construct($translator = null)
public function __construct($translator = null, IntlFormatterInterface $intlFormatter = null)
{
if ($translator instanceof MessageSelector) {
$translator = new IdentityTranslator($translator);
Expand All @@ -35,6 +36,7 @@ public function __construct($translator = null)
}

$this->translator = $translator ?? new IdentityTranslator();
$this->intlFormatter = $intlFormatter ?? new IntlFormatter();
}

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

/**
* {@inheritdoc}
*/
public function formatIntl(string $message, string $locale, array $parameters = array()): string
{
return $this->intlFormatter->formatIntl($message, $locale, $parameters);
}

/**
* {@inheritdoc}
*
Expand Down
Loading
0