barryvdh / laravel-translation-manager

Manage Laravel translation files
MIT License
1.64k stars 419 forks source link

Array format generating an exception: "ErrorException: htmlspecialchars() expects parameter 1 to be string, array given ..." #153

Closed christiansyl closed 7 years ago

christiansyl commented 7 years ago

Hi all.

I am using Laravel 5.3 with php 7.x and my original lang files are all defined using the dot notation, e.g. 'header.menu.experts.find' => 'Find Experts', after exporting the updated translation from the DB (using php artisan translations:export) the files are now using the array format:

return array (
  'header' => 
  array (...

But this is causing my app to crash, i.e. it seems that the array format cannot be read by my application. I get the following exception: ErrorException: htmlspecialchars() expects parameter 1 to be string, array given in ...

There is probably a configuration I can set somewhere to let Laravel read that notation for the array, instead of the []?

It's probably something that has been adressed before but I can't find any doc or issues that address this problem.

Thanks,

Christian

zoltiecodes commented 7 years ago

Hi Christian,

What is your full array with the 'header' key? And how do you use it in the trans() function?

If you have an array like this in your language file:

'header' => array( 'menu' => array( 'experts' => array( 'find' => 'Find Experts', ... ) ) ), (I don't know how to put them into new lines here, sorry)

Then you can get the text by: trans('fileName.header.menu.experts.find');

christiansyl commented 7 years ago

The array I described above is in a file called app.php. So I am using the following to get the translation: trans('app.header.menu.experts.find') Its working fine when my language file has the following format:

return [
'header.menu.experts.find' => 'Find Experts',
...
]

But its giving me the exception described above when my language file has the following format (this is the output from the script).

return array (
  'header' => 
  array (
    'joinExpert' => 'Sluiten als expert',
    'login' => 'Log in',
    'register' => 'Registreren',
    'menu' => 
    array (
      'dashboard' => 'Dashboard',
      'experts' => 
      array (
        'find' => 'Vind Experts',
        'findServices' => 'Vind Experts Services',
      ),
      'conversations' => 'conversaties',
      'projects' => 
      array (
        'overview' => 'project Overzicht',
        'new' => 'Create New Project',
        'bidding' => 'Blader Beschikbaar Project voor Bidding',
      ),
      'finance' => 'Finance
',
      'services' => 'diensten',
      'bids' => 'biedingen',
    ),
  ),

  ...
  );
christiansyl commented 7 years ago

Finally, after some debugging I found out the source of the problem.... All the details are available in that post on Stack Overflow: http://stackoverflow.com/questions/40933493/laravel-localization-file-format-error-array-versus-format