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

Appearance settings

[Translator] Intl message converter #28486

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

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -113,6 +113,13 @@
<tag name="console.command" command="translation:update" />
</service>

<service id="console.command.translation_icu_convert" class="Symfony\Component\Translation\Command\IntlConvertCommand">
<argument type="service" id="translation.writer" />
<argument type="service" id="translation.reader" />
<tag name="console.command" command="translation:convert-to-icu-messages" />
</service>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be moved to the new PHP config file.



<service id="console.command.workflow_dump" class="Symfony\Bundle\FrameworkBundle\Command\WorkflowDumpCommand">
<tag name="console.command" command="workflow:dump" />
</service>
Expand Down
96 changes: 96 additions & 0 deletions 96 src/Symfony/Component/Translation/Command/IntlConvertCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?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\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Translation\Reader\TranslationReaderInterface;
use Symfony\Component\Translation\Util\IntlMessageConverter;
use Symfony\Component\Translation\Writer\TranslationWriterInterface;

/**
* Convert to Intl styled message format.
*
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class IntlConvertCommand extends Command
{
protected static $defaultName = 'translation:convert-to-icu-messages';

private $writer;
private $reader;

public function __construct(TranslationWriterInterface $writer, TranslationReaderInterface $reader)
{
parent::__construct();

$this->writer = $writer;
$this->reader = $reader;
}

/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setDescription('Convert from Symfony 3 plural format to ICU message format.')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove 3 here.

->addArgument('locale', InputArgument::REQUIRED, 'The locale')
->addArgument('path', null, 'A file or a directory')
->addOption('domain', null, InputOption::VALUE_OPTIONAL, 'The messages domain')
->addOption('output-format', null, InputOption::VALUE_OPTIONAL, 'Override the default output format', 'xlf')
;
}

protected function execute(InputInterface $input, OutputInterface $output)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

: int

{
$io = new SymfonyStyle($input, $output);
$path = $input->getArgument('path');
$locale = $input->getArgument('locale');
$domain = $input->getOption('domain');
/** @var KernelInterface $kernel */
$kernel = $this->getApplication()->getKernel();

// Define Root Paths
$transPaths = $kernel->getProjectDir().'/translations';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$transPaths = $kernel->getProjectDir().'/translations';
🔽
$transPaths = $kernel->getProjectDir().DIRECTORY_SEPARATOR.'translations';
WDYT ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you

if (null !== $path) {
$transPaths = $path;
}

// load any existing messages from the translation files
$currentCatalogue = new MessageCatalogue($locale);
if (!is_dir($transPaths)) {
throw new \LogicException('The "path" must be a directory.');
}
$this->reader->read($transPaths, $currentCatalogue);

$allMessages = $currentCatalogue->all($domain);
if (null !== $domain) {
$allMessages = array($domain => $allMessages);
}

$updated = array();
foreach ($allMessages as $messageDomain => $messages) {
foreach ($messages as $key => $message) {
$updated[$messageDomain][$key] = IntlMessageConverter::convert($message);
}
}

$this->writer->write(new MessageCatalogue($locale, $updated), $input->getOption('output-format'), array('path' => $transPaths));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return 0;

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

declare(strict_types=1);

namespace Symfony\Component\Translation\Tests\Util;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Translation\Util\IntlMessageConverter;

class IntlMessageConverterTest extends TestCase
{
/**
* @dataProvider getTestData
*/
public function testConvert($input, $output)
{
$result = IntlMessageConverter::convert($input);
$this->assertEquals($output, $result);
}

/**
* We cannot use negative Inf together with positive Inf.
*/
public function testImpossibleConvert()
{
$this->expectException(\LogicException::class);
IntlMessageConverter::convert(']-Inf, -2[ Negative|]1,Inf[ Positive');
}

public function getTestData()
{
yield array(
'{0} There are no apples|{1} There is one apple|]1,Inf[ There %name% are %count% apples',
<<<ICU
{ COUNT, plural,
=0 {There are no apples}
=1 {There is one apple}
other {There {name} are # apples}
}
ICU
);
yield array('foo', 'foo');
yield array('Hello %username%', 'Hello {username}');

yield array(
']-7, -2] Negative|[2, 7] Small|]10,Inf[ Many',
<<<ICU
{ COUNT, plural,
=-6 {Negative}
=-5 {Negative}
=-4 {Negative}
=-3 {Negative}
=-2 {Negative}
=2 {Small}
=3 {Small}
=4 {Small}
=5 {Small}
=6 {Small}
=7 {Small}
other {Many}
}
ICU
);

// Test overlapping, make sure we have the same behaviour as Symfony
yield array(
'[2,5] Small|]3,Inf[ Many',
<<<ICU
{ COUNT, plural,
=2 {Small}
=3 {Small}
=4 {Small}
=5 {Small}
other {Many}
}
ICU
);
}
}
116 changes: 116 additions & 0 deletions 116 src/Symfony/Component/Translation/Util/IntlMessageConverter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?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\Util;

/**
* Convert from Symfony 3's plural syntax to Intl message format.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove the reference to 3 here as well.

* {@link https://messageformat.github.io/messageformat/page-guide}.
*
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class IntlMessageConverter
{
public static function convert(string $message): string
{
$array = self::getMessageArray($message);

if (1 === \count($array) && isset($array[0])) {
return preg_replace('|%(.*?)%|s', '{$1}', $message);
}

$icu = self::buildIcuString($array);

return $icu;
}

/**
* Get an ICU like array.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gets

*/
private static function getMessageArray(string $message): array
{
$parts = array();
if (preg_match('/^\|++$/', $message)) {
$parts = explode('|', $message);
} elseif (preg_match_all('/(?:\|\||[^\|])++/', $message, $matches)) {
$parts = $matches[0];
}

$intervalRegexp = <<<'EOF'
/^(?P<interval>
({\s*
(\-?\d+(\.\d+)?[\s*,\s*\-?\d+(\.\d+)?]*)
\s*})

|

(?P<left_delimiter>[\[\]])
\s*
(?P<left>-Inf|\-?\d+(\.\d+)?)
\s*,\s*
(?P<right>\+?Inf|\-?\d+(\.\d+)?)
\s*
(?P<right_delimiter>[\[\]])
)\s*(?P<message>.*?)$/xs
EOF;

$standardRules = array();
foreach ($parts as $part) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if $parts is empty ? I would have add a check not to continue if empty($parts) is true

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont know.. When will parts be empty?

I adding a small fix.

$part = trim(str_replace('||', '|', $part));

// try to match an explicit rule, then fallback to the standard ones
if (preg_match($intervalRegexp, $part, $matches)) {
if ($matches[2]) {
foreach (explode(',', $matches[3]) as $n) {
$standardRules['='.$n] = $matches['message'];
}
} else {
$leftNumber = '-Inf' === $matches['left'] ? -INF : (float) $matches['left'];
$rightNumber = \is_numeric($matches['right']) ? (float) $matches['right'] : INF;

$leftNumber = ('[' === $matches['left_delimiter'] ? $leftNumber : 1 + $leftNumber);
$rightNumber = (']' === $matches['right_delimiter'] ? 1 + $rightNumber : $rightNumber);

if ($leftNumber !== -INF && INF !== $rightNumber) {
for ($i = $leftNumber; $i < $rightNumber; ++$i) {
$standardRules['='.$i] = $matches['message'];
}
} else {
// $rightNumber is INF or $leftNumber is -INF
if (isset($standardRules['other'])) {
throw new \LogicException(sprintf('%s does not support converting messages with both "-Inf" and "Inf". Message: "%s"', __CLASS__, $message));
}
$standardRules['other'] = $matches['message'];
}
}
} elseif (preg_match('/^\w+\:\s*(.*?)$/', $part, $matches)) {
$standardRules[] = $matches[1];
} else {
$standardRules[] = $part;
}
}

return $standardRules;
}

private static function buildIcuString(array $data): string
{
$icu = "{ COUNT, plural,\n";
foreach ($data as $key => $message) {
$message = strtr($message, array('%count%' => '#'));
$message = preg_replace('|%(.*?)%|s', '{$1}', $message);
$icu .= sprintf(" %s {%s}\n", $key, $message);
}
$icu .= '}';

return $icu;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.