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

Skip to content

[Translation] Default parameters #48182

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
sylfabre opened this issue Nov 9, 2022 · 7 comments Β· Fixed by #53425
Closed

[Translation] Default parameters #48182

sylfabre opened this issue Nov 9, 2022 · 7 comments Β· Fixed by #53425

Comments

@sylfabre
Copy link
Contributor

sylfabre commented Nov 9, 2022

Description

Hello

First of all, thank you for this amazing framework πŸ™

When using translation strings with params in large applications, we keep injecting the same params:

  • a user's name, first, or last name
  • a brand name
  • an application name
  • a customer's name
  • ...

And it's pretty difficult to enforce a strict naming convention.
For instance, we can have these variations about a user's name:

  • user_name
  • username
  • userName
  • name

It would be nice to have a way to define a set of common params that don't need to be injected every time the trans() method is invoked within PHP code or a Twig template.

Thanks!

Example

setDefaultParams([ 'first_name' => 'Sylvain', 'company' => 'AssoConnect', ]); // hello: 'Hello {first_name}, you work at {company} $translor->trans('hello'); // Hello Sylvain, you work at AssoConnect
@94noni
Copy link
Contributor

94noni commented Nov 10, 2022

hi @sylfabre

at work we do have a decorator of the translator, in which we inject a list of global key/value (mimic of twig global)

It would be definitely great to have thing in symfony core as default empty but configureable

I can provide a basic PR as well :)

@TheDevick
Copy link

Heey!
Is this feature will be implemented? Like I'm having this trouble, where I wish I can set like:

  • %open_bold% / %close_bold% - To open and close tags to be bold, apply the styles...

The Boring Situation

And it's boring do everytime, in my Twig templates, like:

Message

route.index.title.text: We are %open_bold%Company Name%close_bold%

Twig

{{ 'route.index.title.text'|trans({'%open_bold%': '<span class="fw-bold">', '%close_bold%': '</span>'})|raw }}

@carsonbot
Copy link

Thank you for this issue.
There has not been a lot of activity here for a while. Has this been resolved?

@carsonbot
Copy link

Could I get a reply or should I close this?

@TheDevick
Copy link

Hi!
I don't think you should close this; this is not implemented yet in Symfony, but I think it would be nice to be implemented.

@carsonbot carsonbot removed the Stalled label Sep 22, 2023
@Jean-Beru
Copy link
Contributor

I also think that it could be a great feature for the Translation component.

@94noni, did you start a PR ? I can work on it if you're OK.

@94noni
Copy link
Contributor

94noni commented Jan 3, 2024

@Jean-Beru no i didnt push any thing of that sort, of course I am ok :)
feel free to ping me for review or even code sharing !
cheers


here it is, nothing fancy, mimic twig global as mentioned on my initial comment

    // core internal translator decorating symfony translator
    private array $globalParameters = [];

    public function setGlobalParameters(array $parameters): void
    {
        $this->globalParameters = \array_replace($this->globalParameters, $parameters);
    }

    public function trans(string $id, array $parameters = [], string $domain = null, string $locale = null): string
    {
        // Here we inject global translation parameters
        // Notice the using of array_merge, so global parameter is overwrite-able
        return $this->decoratedTranslator->trans($id, \array_merge($this->globalParameters, $parameters), $domain, $locale);
    }

and in a compiler pass we inject some staticaly/dynamicaly generated trans params

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants