-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
Comments
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 :) |
Heey!
The Boring SituationAnd it's boring do everytime, in my Twig templates, like: Messageroute.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 }} |
Thank you for this issue. |
Could I get a reply or should I close this? |
Hi! |
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. |
@Jean-Beru no i didnt push any thing of that sort, of course I am ok :) 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 |
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:
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 AssoConnectThe text was updated successfully, but these errors were encountered: