Laravel本地化文件格式错误:array()与[]格式

时间:2016-12-02 13:36:52

标签: laravel-5 localization php-7 laravel-localization

我在Laravel 5.3(使用php 7)中进行了本地化处理。 Laravel 5.3中的默认localizaiton文件格式使用括号,如下例所示:

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

这就是我在我的应用程序中使用的,它工作正常。但现在我正在尝试使用一些软件包来帮助翻译,例如:

但是这两个都以“旧”laravel 4.x数组格式生成本地化文件。例如

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

据我了解,我的laravel 5.3应用程序中的本地化文件格式应该没有问题,但是它总是抛出异常:

[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)

我真的不明白为什么这种格式无法使用我的应用。我敢打赌,我错过了一些微不足道的事情,但任何帮助都会非常受欢迎!

谢谢,

基督教

1 个答案:

答案 0 :(得分:1)

经过几个小时的代码后,我找到了问题的根源。

例如,我在原来的lang文件中得到了这些文件:

'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',

当我尝试使用原始问题中提到的两个软件包时,他们产生了以下输出:

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

正如您所看到的,生成的文件删除了文本footer.subscribe的值,并且仅保留了子元素,intro,privacy,tos和tas。因此,对trans('footer.subscribe')的请求将返回一个数组,而不是文本。

现在我知道了,我将更改原始翻译文件的格式!

下进行。