Skip to content

Replace %count% with a given number out of the box #19795

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 5 commits into from

Conversation

bocharsky-bw
Copy link
Contributor

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

We already have this feature for transchoice Twig filter, but why only for it? It will be consistent to have this for translator in general. We already have a $number parameter in transChoice() which we could easily use for that.

Before

$this->get('translator')
    ->transChoice('1 apple|%count% apples', 7, [
        '%count%' => 7,
    ]);

After:

$this->get('translator')
    ->transChoice('1 apple|%count% apples', 7);

@@ -95,7 +95,7 @@ public function trans($message, array $arguments = array(), $domain = null, $loc

public function transchoice($message, $count, array $arguments = array(), $domain = null, $locale = null)
{
return $this->translator->transChoice($message, $count, array_merge(array('%count%' => $count), $arguments), $domain, $locale);
return $this->translator->transChoice($message, $count, $arguments, $domain, $locale);
Copy link
Contributor

Choose a reason for hiding this comment

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

You need to keep this in case the twig bridge is used with an older version of the translator.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right, thanks!

@@ -198,6 +198,10 @@ public function trans($id, array $parameters = array(), $domain = null, $locale
*/
public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
{
$parameters = array_merge(array(
Copy link

Choose a reason for hiding this comment

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

Why not?
$parameters['%count%'] = $number;

Copy link
Contributor Author

@bocharsky-bw bocharsky-bw Aug 31, 2016

Choose a reason for hiding this comment

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

Good question! Because of this way we allow devs to override this parameter with their own value. I mean something like this:

print $this->get('translator')
    ->transChoice('1 apple|%count% apples', 7, [
        '%count%' => 'no'
    ]); // will print "no apples"

Copy link
Contributor

@ogizanagi ogizanagi Aug 31, 2016

Choose a reason for hiding this comment

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

What about:

$parameters += array('%count%' => $number);

?

Copy link
Contributor

Choose a reason for hiding this comment

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

@ogizanagi See this comment: #7029 (comment)

@ogizanagi
Copy link
Contributor

This new behavior can be tested by updating the TranslatorTest::getTransChoiceTests and removing %count% from the given parameters.
Also a new tests case should ensure that the %count% placeholder is overwritable if passed as parameter :)

@bocharsky-bw
Copy link
Contributor Author

Hey @ogizanagi , thanks for your help!

@fabpot
Copy link
Member

fabpot commented Sep 14, 2016

Thank you @bocharsky-bw.

@fabpot fabpot closed this Sep 14, 2016
fabpot added a commit that referenced this pull request Sep 14, 2016
…charsky-bw)

This PR was squashed before being merged into the 3.2-dev branch (closes #19795).

Discussion
----------

Replace %count% with a given number out of the box

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

We already have this feature for [transchoice](https://github.com/symfony/symfony/blob/master/src/Symfony/Bridge/Twig/Extension/TranslationExtension.php#L98) Twig filter, but why only for it? It will be consistent to have this for translator in general. We already have a `$number` parameter in `transChoice()` which we could easily use for that.

Before
```php
$this->get('translator')
    ->transChoice('1 apple|%count% apples', 7, [
        '%count%' => 7,
    ]);
```

After:
```php
$this->get('translator')
    ->transChoice('1 apple|%count% apples', 7);
```

Commits
-------

4c1a65d Replace %count% with a given number out of the box
@bocharsky-bw bocharsky-bw deleted the patch-1 branch September 14, 2016 20:46
@fabpot fabpot mentioned this pull request Oct 27, 2016
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