0

I want to localize my app, but I got a problem:

If I call the trans function like this: {{ __('permission.addUser') }}, the output is

Add new user

If I call {{$permissions[13]}} the output is

addUser

But if I mix them: {{ __('permission.$permission[13]') }}, the output is

permission.$permissions[13]

What's missing? What I did wrong?

Iosu
  • 213
  • 7
  • 14
Feralheart
  • 1,881
  • 5
  • 28
  • 59
  • 1
    After that, read [What is the difference between single-quoted and double-quoted strings in PHP](https://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php.) – ceejayoz Mar 05 '18 at 21:22
  • @ceeyajoz The parameter in that case is very far from my question. And laravel-blade don't allow double quotes – Feralheart Mar 06 '18 at 06:27
  • Laravel Blade most certainly **does** permit double-quotes. – ceejayoz Mar 06 '18 at 13:41

2 Answers2

1

I know you already have your answer, but in PHP, you can use double quotes to insert variables into strings, for example

echo "permission.$permissions_13"

will output correctly. If you want to use arrays or objects, you should wrap them in {}.

__("permission.{$permissions[13]}")

Remember, double quotes

Juan Pablo
  • 363
  • 2
  • 9
  • 1
    Thank you for your answer. In "flat php" I use the double quotes in a lot of situations, but laravel-blade somewhy can't "eat" them. It drop's this error: > Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting '-' or identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) – Feralheart Mar 06 '18 at 06:25
  • Oh, I had no idea. Weird. Then yes, your solution sounds correct, string concat :) – Juan Pablo Mar 06 '18 at 12:19
0

Oops, I wanted to use my variable as a string.

The solution is {{ __('permission.' . $permissions[13]) }}

Feralheart
  • 1,881
  • 5
  • 28
  • 59