Skip to content

[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

Conversation

Nyholm
Copy link
Member

@Nyholm Nyholm commented Sep 17, 2018

Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets
License MIT
Doc PR

As discussed in #28375, we need a command to convert from "Symfony 3" plural format to Intl message format.

Before

<trans-unit id="xx0" resname="simple_key">
  <source>simple_key</source>
  <target>Hello %name%</target>
</trans-unit>
<trans-unit id="xx1" resname="plural_key">
  <source>plural_key</source>
  <target>{0} %name%, there are no apples|{1} %name%, there is one apple|]1,Inf] %name%, there are %count% apples</target>
</trans-unit>
{{ 'simple_key|trans({"%name%": "foo"}) }} <br>
{{ 'plural_key|transchoice(2, {"%name%": "foo"}) }}

After

<trans-unit id="xx0" resname="simple_key">
  <source>simple_key</source>
  <target>Hello {name}</target>
</trans-unit>
<trans-unit id="xx1" resname="plural_key">
  <source>plural_key</source>
  <target>{ COUNT, plural,
    =0 {{name}, there are no apples}
    =1 {{name}, there is one apple}
    other {{name}, there are # apples}
}</target>
{{ 'simple_key'|trans({"name": "foo"}) }} <br>
{{ 'plural_key'|trans({"COUNT":2, "name": "foo"}) }}

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.

$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

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.

@Nyholm Nyholm changed the title [Translator] WIP: Intl message converter [Translator] WIP: Icu message converter Sep 22, 2018
@Nyholm Nyholm changed the title [Translator] WIP: Icu message converter [Translator] Icu message converter Sep 22, 2018
@Nyholm Nyholm changed the title [Translator] Icu message converter [Translator] Intl message converter Sep 22, 2018
@nicolas-grekas
Copy link
Member

If #28375 goes well, we might close this one and save us some work. \o/

@fabpot
Copy link
Member

fabpot commented Oct 10, 2018

Why could we close this one? We still need a way to move old messages to the new format, right?

@nicolas-grekas
Copy link
Member

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.

@fabpot
Copy link
Member

fabpot commented Oct 10, 2018

I get that, but I think people will want to migrate to the new format as it is more powerful.

@stof
Copy link
Member

stof commented Oct 11, 2018

@nicolas-grekas at least for pluralized message, it is deprecated currently (as APIs consuming it are deprecated)

@nicolas-grekas
Copy link
Member

@stof not really, see #28375 (comment)

Copy link
Member

@nicolas-grekas nicolas-grekas left a 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.

@fabpot
Copy link
Member

fabpot commented Dec 3, 2019

@Nyholm Do you have any plans to finish this one? I think it would be a very nice and useful addition.

@Nyholm
Copy link
Member Author

Nyholm commented Dec 3, 2019

Yes, I will make sure to complete this PR.

Thank you for the ping

@welcoMattic
Copy link
Member

This PR could be very useful alongside #37462 to automate intl conversion of messages stored in a translation SaaS.
@Nyholm do you plan to finish it before 5.2 release (which is the release we target for #37462)?

@Nyholm
Copy link
Member Author

Nyholm commented Jul 1, 2020

Yes. I will

Copy link
Member

@fabpot fabpot left a 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>
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.

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.

;
}

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

}
}

$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;

}

/**
* 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

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.

public static function convert(string $message, string $variableDelimiter = '%'): string
{
$array = self::getMessageArray($message);
if (empty($array)) {
Copy link
Member

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);
Copy link
Member

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);
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 inline the regexp

@nicolas-grekas
Copy link
Member

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).

@nicolas-grekas nicolas-grekas modified the milestones: next, 5.2 Oct 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants