Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Translation] Add parameters to DataCollectorTranslator #15139

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 10 commits into from
Closed

[Translation] Add parameters to DataCollectorTranslator #15139

wants to merge 10 commits into from

Conversation

damienalexandre
Copy link
Contributor

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

The new translation web profiler panel miss a important piece of information: the parameters that can be used for placeholders.

This PR add parameters in the collected translations and display them in the WebProfiler translation list.

image

Original idea by my team-mate @joelwurtz 🤘

@jjsaunier
Copy link

👌

@aitboudad
Copy link
Contributor

👍 looks good, but for 2.8

@@ -59,7 +59,7 @@ public function trans($id, array $parameters = array(), $domain = null, $locale
public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
{
$trans = $this->translator->transChoice($id, $number, $parameters, $domain, $locale);
$this->collectMessage($locale, $domain, $id, $trans);
$this->collectMessage($locale, $domain, $id, $trans, $parameters);
Copy link
Contributor

Choose a reason for hiding this comment

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

we need to pass the $number too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, didn't done it because the Twig extension copy the number to the parameters already (see here),
but when using the translator directly, this is going to be an issue. I will update my commit soon.

@damienalexandre
Copy link
Contributor Author

PR updated with the addition of transChoice (capture image updated in the main post).

It look like I must close this PR and re-open on 2.8?

@stof
Copy link
Member

stof commented Jun 30, 2015

@damienalexandre we can rebase it on top of 2.8 at merging time with our merging tool (assuming there is no conflict), so it is not necessary. This would mark the PR as closed rather than merged though because of the rebase

<code>{{ key }} = {{ value }}</code><br />
{% endfor %}
{% if message.transChoiceNumber is not null %}
<em><code>transChoice count = {{ message.transChoiceNumber }}</code></em>
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 rather say pluralization than transchoice Count

@stof
Copy link
Member

stof commented Jun 30, 2015

How does it behave when the same key is translated several times but with different parameter values (and potentially even different parameters) and a different pluralization count ?

@damienalexandre
Copy link
Contributor Author

This code:

<p>{{ 'demo.homepage.introduction'|trans({'%username%': 'Damien'}) }}</p>
<p>{{ 'demo.homepage.introduction'|trans({'%username%': 'Toto'}) }}</p>

produce this output:

image

Only the first call is kept (see TranslationDataCollector::sanitizeCollectedMessages), so we only have the parameters used in the first one. For this reason I'm not sure about using =, because it's more like "could be this value"... Two solutions:

  • I improve sanitizeCollectedMessages to keep all the parameters and I display them all
  • I change the = in the view to reflect the fact this is only an extract, any idea how to do that?

What do you think?

@stof
Copy link
Member

stof commented Jul 1, 2015

@damienalexandre I'm actually wondering whether the value of the parameter should really be kept here (same for the pluralization count). What we really care about IMO is which parameters are available, and whether it is pluralized or no.

@stof
Copy link
Member

stof commented Jul 1, 2015

keeping all parameters values would also look confusing IMO

@aitboudad
Copy link
Contributor

Instead of adding a new column I would propose grouping parameters like we did in doctrine which can fix the confusion

selection_026

@damienalexandre
Copy link
Contributor Author

Having the parameters values proved to be really helpful on the project I'm working on.
For example you could need to check that a date is properly formatted before the translation actually use the key:
image

So I'm in favor of @aitboudad idea, specially because the folding table allow to display lots of information's without breaking the display to much.

Here is what I've just pushed. For this code:

<p>{{ 'demo.homepage.hello'|trans }}</p>
<p>{{ 'demo.homepage.user_friends'|transchoice(5, {'%name%': 'Fabien'}) }}</p>
<p>{{ 'demo.homepage.user_friends'|transchoice(0, {'%name%': 'Fabien'}) }}</p>
<p>{{ 'demo.homepage.time'|trans({'%datetime%': 'now'|date}) }}</p>
<p>{{ 'demo.homepage.introduction'|trans({'%username%': 'Damien'}) }}</p>
<p>{{ 'demo.homepage.introduction'|trans({'%username%': 'Toto', '%foo%': 'bar'}) }}</p>

It produce this in the translation panel:

image

Expended it looks like this:

image

As you can see I use the dump function,
not sure this is accepted but the colors and the display are very very good, I wish more panels use this function to display data.

Let my know what you think,
cheers 🤘

@fabpot
Copy link
Member

fabpot commented Jul 4, 2015

@damienalexandre Using dump() everywhere in the profiler would be a huge win! That would also allow to deprecate and then remove a bunch of code. I like that! Can you work on that?

{% if message.transChoiceNumber is not null %}<br><small style="color: gray;">(use pluralization)</small>{% endif %}

<div>
[<a href="#" onclick="return openParameters(this);" style="text-decoration: none;"
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's better to hide this link when parameters is empty

Copy link
Contributor Author

Choose a reason for hiding this comment

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

message.parameters is never empty, and reflect all the calls to the given translation key. So it's perfectly possible to have:

  • one first call with params: {"%foo%": "bar"}
  • one second call without: null
  • one third with other params: {"%bar%": "foo"}

If it's called 5 times without parameters, it will display 5 times null.

@aitboudad
Copy link
Contributor

using dump() everywhere should be done in another PR.

@damienalexandre
Copy link
Contributor Author

Ok so I replaced my dump by profiler_dump, and another PR will take care of replacing all the profiler_dump and remove the code needed for it.

Here is what is looks like now:

image

@aitboudad aitboudad added the Ready label Jul 6, 2015
@aitboudad
Copy link
Contributor

ping @symfony/deciders

@fabpot
Copy link
Member

fabpot commented Jul 6, 2015

👍

@xabbuh
Copy link
Member

xabbuh commented Jul 7, 2015

Should we really include the parameters part if there were no parameters passed to trans()? Imho it is confusing to then see null there.

@javiereguiluz
Copy link
Member

I agree with @xabbuh: displaying null when there are no parameters looks like an error (made by Symfony or our application).

@aitboudad aitboudad removed the Ready label Jul 7, 2015
@damienalexandre
Copy link
Contributor Author

Ok, I mentioned before why I made it this way,
I need to find a way to make sure all the calls for a specific message don't have any parameters. Thanks for the feedbacks!

@xabbuh
Copy link
Member

xabbuh commented Jul 7, 2015

Maybe it's sufficient to just show a message telling "no parameters" or something similar.

@damienalexandre
Copy link
Contributor Author

Here we go; I've change the way I store the parameters in the DataCollector to allow a better display.

Here is an example where the first call does not have parameters, so we do not display the expend link, and where the last translation is called 3 times but only have parameters 2 times:

image

Looks better 👍 Good feedback 😉

@fabpot
Copy link
Member

fabpot commented Jul 9, 2015

Thank you @damienalexandre.

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.

7 participants