Handle dynamic translations based on current controller/action.
Allows your application to use dynamic translations with ease, (*1)
Run the following to include this via Composer, (*2)
composer require waterloomatt/translation
Register the service provider with your application by adding the following line to config/app.php, (*3)
'providers' => [ Waterloomatt\Translation\Providers\TranslationServiceProvider::class
That's it! You're good to go., (*4)
In your views, use Laravel's translation helper as you normally would. Nothing needs to change here., (*5)
{{ trans('messages.pageTitle') }}
Now, the fun bit! In this example, you can override the 'pageTitle' translation by specifying which controller or action (or both) a translation should apply to., (*6)
In your messages.php translation file, override the pageTitle
translation by specifying a controller or action (or both)., (*7)
return [ 'pageTitle' => 'My Application!' // Applies to all pages 'controller:search_key:pageTitle' => 'Search', // Applies to /search/{any_action} 'action:index_key:pageTitle' => 'All Index Pages!',// Applies to /{any_controller}/index 'controller:user_action:update_key:pageTitle' => 'Update User', // Applies to user/update ];
As you can see, you can control which translation will be used by specifying the controller or action or both. The most specific translation that matches the current route will be used., (*8)
controller:user
, controller:payment
controller:user_action:update
, controller:payment_action:decline_key:pageTitle
In the following examples, assume your current route is http://localhost/public/search/index, (*9)
return [ 'controller:search_key:pageTitle' => 'Search', 'controller:search_action:index_key:pageTitle' => 'Search', // this one! ];
return [ 'action:index_key:pageTitle' => 'Search', 'pageTitle' => 'Search', // this one! ];
return [ 'controller:search_action:result_key:pageTitle' => 'Search', 'controller:search_action:index_key:pageTitle' => 'Search', // this one! ];
return [ 'controller:search_key:pageTitle' => 'Search', 'action:index_key:pageTitle' => 'Search', // this one! ];