laravel / ideas

Issues board used for Laravel internals discussions.
938 stars 28 forks source link

“Global” translation parameters #1486

Open martinbean opened 5 years ago

martinbean commented 5 years ago

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 :club parameter 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 :team parameter. It’s therefore getting pretty cumbersome to specify these parameters for every translation, i.e.

@section('title', __(':team | Teams | :club', ['team' => $team->name, 'club' => $club->name]))

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:

class SomeMiddleware
{
    public function handle($request, Closure $next)
    {
        Lang::parameter(':club', $request->route('club')->name);
        Lang::parameter(':team', $request->route('team')->name);

        return $next($request);
    }
}
<!-- resources/views/club/team/show.blade.php -->
@section('title', trans(':team | Teams | :club'))
<!-- i.e. "First Team | Teams | Manchester United" -->

In the above, the :team and :club parameters will be replaced with whatever values are set using the Lang façade in the example middleware class.

martinbean commented 5 years ago

Thoughts, @taylorotwell?