mardi 24 janvier 2017

Laravel localization: optional placeholders

In Laravel, you can use placeholders in localization files, so that if you have a welcome.php file set up with the following rule:

'welcome' => 'Welcome, :name. Everything alright in :city?',

and you use this to print out a message:

echo trans('messages.welcome', ['name' => 'Dayle', 'city' => 'Boston']);

you will see this:

Hello, Dayle! Everything alright in Boston?

Here :name is a placeholder. My question is: can you make these placeholders optional, i.e. if their value is null, a different text will be displayed? In the above case, if we skipped the city argument in the trans function, I would like to see this:

Hello, Dayle!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire