Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parameters inside Laravel localized string

Tags:

php

laravel

I'd like have something like this in the lang/en/mymsgs.php

'string1' => 'Welcome %1, Please meet %2' 

I would provide the content for %1 and %2 when getting the 'string1' from my code.

I couldn't find a way to do this. Any pointers?

like image 846
JasonGenX Avatar asked May 14 '15 16:05

JasonGenX


2 Answers

Laravel message localization uses named, not numeric, parameters.

Rewriting your example message:

'string1' => 'Welcome :user, Please meet :other', 

You can now use, for example:

trans('string1', [ 'user' => 'Ainsley', 'other' => 'Hayden' ]); 
like image 137
bishop Avatar answered Sep 26 '22 01:09

bishop


If anyone use string translation for Laravel. They can use like this -> __('Some translatable string with :attribute',['attribute' => $attribute_var])

check the documentation for more https://laravel.com/docs/5.7/localization#using-short-keys

like image 33
Tamim Avatar answered Sep 24 '22 01:09

Tamim