user48594
user48594

Reputation: 88

Laravel localization file format error: array() versus [] format

I am struggling a bit with localization in Laravel 5.3 (with php 7). The default localizaiton file format in Laravel 5.3 is using brackets, as in this example:

return [
'footer.contact.email'   => 'Email:',
]

That's what I have been using in my app and it's working fine. But now I am trying to work with some packages to help with translations, for example:

But both of those generate localization files in the "old" laravel 4.x array format. For example

return array(
  'footer' => array(
    'contact' => array(
      'email' => 'Email:',
    ),
   ), 
);

As I understand it I should have no issue with this localization file format in my laravel 5.3 app, however it's always throwing an exception:

[2016-12-02 13:26:01] local.ERROR: ErrorException: htmlspecialchars() expects parameter 1 to be string, array given in C:\100_source_code\consulting_platform_laravel\maingig\vendor\laravel\framework\src\Illuminate\Support\helpers.php:519
Stack trace:
#0 C:\100_source_code\consulting_platform_laravel\maingig\vendor\sentry\sentry\lib\Raven\Breadcrumbs\ErrorHandler.php(36): Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(2, 'htmlspecialchar...', 'C:\\100_source_c...', 519, Array)

I really cant understand why this format is not working with my app. I bet it is something trivial that I am missing, but any help would be very welcome!

Thanks,

Christian

Upvotes: 0

Views: 486

Answers (1)

user48594
user48594

Reputation: 88

After a few extra hours of stepping through the code I found the source of the problem.

For example, I got these in my original lang file:

'footer.subscribe'   => 'SUBSCRIBE TO OUR NEWSLETTER',
'footer.subscribe.intro'   => 'Be the first to know about our latest news...',
'footer.subscribe.privacy'   => 'Privacy Policy',
'footer.subscribe.tos'   => 'Terms of Service',
'footer.subscribe.tac'   => 'Terms and Conditions',

As I tried to use both of the packages mentioned in my original question they produced the following output:

'footer' => 
  array (
    'subscribe' => 
    array (
      'intro' => 'TODO: intro',
      'privacy' => 'TODO: privacy',
      'tos' => 'TODO: tos',
      'tac' => 'TODO: tac',
    ),
  ),

As you can see the generated file dropped the value for the text footer.subscribe and only kept the child element, intro, privacy, tos and tas in this case. Therefore a request for trans('footer.subscribe') returns an array and not the text.

Now that I know this I will change the format of my original translation file!

c.

Upvotes: 1

Related Questions