“Default” translation parameters #40263
Replies: 4 comments
-
|
I would definitely find this a useful way to clean using parameters everywhere. |
Beta Was this translation helpful? Give feedback.
-
|
Well, I'm going through a similar pain as we speak, so definitely a thumbs up for this. |
Beta Was this translation helpful? Give feedback.
-
|
please do share with us how and where this can be done. |
Beta Was this translation helpful? Give feedback.
-
|
For anyone else who ends up here, a quickfix that worked for me until this is a part of the core: AppServiceProvider.php public function register() {
$this->app->extend('translator', function (Translator $translator) {
return new DefaultParamTranslator($translator->getLoader(), $translator->getLocale());
});
}DefaultParamTranslator.php use Illuminate\Translation\Translator;
class DefaultParamTranslator extends Translator {
public function get($key, array $replace = [], $locale = null, $fallback = true)
{
return parent::get($key, [
'company_name' => 'Laravel',
...$replace
], $locale, $fallback);
}
}Open for better suggestions, quite new to container bindning |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
What do people think of specifying parameter replacements outside the normal flow of picking a string?
I’m using translations for page titles and descriptions for a sports-related site. Pretty much every string has a
:clubparameter that’s replaced with the name of a sports club. A sports club can then have multiple teams, so a lot of titles/descriptions also have a:teamparameter. It’s therefore getting pretty cumbersome to specify these parameters for every translation, i.e.Especially when titles/descriptions include more and more parameters.
Therefore, my thinking is it would be good to be able to specify replacements elsewhere in an application, i.e. a controller or middleware:
In the above, the
:teamand:clubparameters will be replaced with whatever values are set using theLangfaçade in the example middleware class.I’ve achieved this myself by extending the
Translatorbinding in the container so already have a working solution in applications, so would also be happy to contribute the code by way of a PR and document it.Beta Was this translation helpful? Give feedback.
All reactions