-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
Conversation
47846bb
to
6ac56d3
Compare
$kernel = $this->getApplication()->getKernel(); | ||
|
||
// Define Root Paths | ||
$transPaths = $kernel->getProjectDir().'/translations'; |
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you
EOF; | ||
|
||
$standardRules = array(); | ||
foreach ($parts as $part) { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
If #28375 goes well, we might close this one and save us some work. \o/ |
Why could we close this one? We still need a way to move old messages to the new format, right? |
We don't need one because we don't plan to deprecate the current format. It could be nice to provide the command if ppl want to move to intl, but there is no real reason to do it so I'm not sure it's worth it. |
I get that, but I think people will want to migrate to the new format as it is more powerful. |
@nicolas-grekas at least for pluralized message, it is deprecated currently (as APIs consuming it are deprecated) |
@stof not really, see #28375 (comment) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please rebase and move to short arrays.
@Nyholm Do you have any plans to finish this one? I think it would be a very nice and useful addition. |
Yes, I will make sure to complete this PR. Thank you for the ping |
Yes. I will |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Quick review (mostly about style) as a nice ping/reminder :)
<argument type="service" id="translation.writer" /> | ||
<argument type="service" id="translation.reader" /> | ||
<tag name="console.command" command="translation:convert-to-intl-messages" /> | ||
</service> |
There was a problem hiding this comment.
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.
protected function configure() | ||
{ | ||
$this | ||
->setDescription('Convert from Symfony 3 plural format to ICU message format.') |
There was a problem hiding this comment.
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.
; | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
: int
} | ||
} | ||
|
||
$this->writer->write(new MessageCatalogue($locale, $updated), $input->getOption('output-format'), array('path' => $transPaths)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return 0;
} | ||
|
||
/** | ||
* Get an ICU like array. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gets
namespace Symfony\Component\Translation\Util; | ||
|
||
/** | ||
* Convert from Symfony 3's plural syntax to Intl message format. |
There was a problem hiding this comment.
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.
public static function convert(string $message, string $variableDelimiter = '%'): string | ||
{ | ||
$array = self::getMessageArray($message); | ||
if (empty($array)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We try to avoid using empty
in Symfony. if (!$array)
seems enough here.
return self::replaceVariables($message, $variableDelimiter); | ||
} | ||
|
||
$icu = self::buildIcuString($array, $variableDelimiter); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
temp var could be removed
|
||
private static function replaceVariables(string $message, string $variableDelimiter): string | ||
{ | ||
$regex = sprintf('|%s(.*?)%s|s', $variableDelimiter, $variableDelimiter); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would inline the regexp
I'm closing here because this staled and apparently this is not critical, since this gathered very little feedback from the community. Let's reopen when someone actually needs this (I'd invite that person to adopt the patch here to make the new PR). |
As discussed in #28375, we need a command to convert from "Symfony 3" plural format to Intl message format.
Before
After
As you can see, the user needs to modify their twig templates, "
%name%
" => "name
". To use "{%name%}
" in the translation message is not valid syntax.TODO
I need to add more tests of plural messages. Feel free to add a comment with a "Symfony style" plural string and I'll add it to the tests.
We also need a way to modify the the
%name%
placeholder. Users may use#name#
or any other arbitrary string.