Skip to content

[Contracts] add TranslatableInterface #38330

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

Merged
merged 1 commit into from
Sep 29, 2020

Conversation

nicolas-grekas
Copy link
Member

Q A
Branch? master
Bug fix? no
New feature? yes
Deprecations? no
Tickets #38319
License MIT
Doc PR -

An alternative to #38328:

  • TranslatableInterface is added to symfony/contracts;
  • Translatable, still in the component, implements it (and is not final as it makes no sense for a value-object)
  • the t() function is kept in the component - it doesn't fit in the contracts IMHO;
  • Translatable::trans() is not static anymore;
  • the domain is nullable instead of defaulting to "messages";
  • the t() function moved in the Symfony\Component\Translation namespace (for reference, the s() function from String is also namespaced.);
  • the Twig extension is made strictly polymorphic: if you pass a Translatable, you cannot pass arguments/domain/count.

@nicolas-grekas nicolas-grekas force-pushed the contracts-translatable-if branch 2 times, most recently from fc04851 to 059cc32 Compare September 28, 2020 12:11
Copy link
Member

@chalasr chalasr left a comment

Choose a reason for hiding this comment

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

Better!

*/
public function trans($message, array $arguments = [], string $domain = null, string $locale = null, int $count = null): string
public function trans($message, $arguments = [], string $domain = null, string $locale = null, int $count = null): string
Copy link
Member

Choose a reason for hiding this comment

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

The new semantics of the $arguments parameter is not covered by a test, is it? I also wonder if this change is really necessary. I find the error message rather confusing that a developer gets when passing an array of arguments to the trans filter (which can be an honest C&P mistake).

Copy link
Member Author

Choose a reason for hiding this comment

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

Test case added as {{ t("Hello")|trans("fr") }}

Not doing this polymorphism means that ppl would be forced to always write
{{ translateble|trans(locale=fr) }} instead of {{ translateble|trans(fr) }} which the current changes allows.

If the issue is the error message, we could improve it. Suggestions welcome :)

@nicolas-grekas nicolas-grekas force-pushed the contracts-translatable-if branch from b5df1f5 to 9224f7a Compare September 28, 2020 13:06
{
if (!class_exists(Translatable::class)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

it could be an anonymous data object, for general compatibility

Copy link
Member Author

Choose a reason for hiding this comment

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

it could but I doubt this would provide many benefits. The dependency on the component is quite a loose one for someone using the extension.

@fabpot
Copy link
Member

fabpot commented Sep 29, 2020

Thank you @nicolas-grekas.

@fabpot fabpot merged commit d5ce0e3 into symfony:master Sep 29, 2020
@nicolas-grekas nicolas-grekas deleted the contracts-translatable-if branch October 2, 2020 13:45
@nicolas-grekas nicolas-grekas modified the milestones: next, 5.2 Oct 5, 2020
@fabpot fabpot mentioned this pull request 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.

7 participants