51

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?

2
  • 2
    :foo instead of %1, and trans('string1', [ 'foo' => 'bar' ]) perhaps? Commented May 14, 2015 at 16:15
  • thanks. if you can write an answer with what you just mentioned her I'll credit you for it. Commented May 14, 2015 at 16:28

3 Answers 3

98

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' ]);
Sign up to request clarification or add additional context in comments.

Comments

31

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

Comments

0

using __('string1', [ 'user' => 'Ainsley', 'other' => 'Hayden' ])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.