php - 使用 oscarotero/gettext 翻译库时无法重新声明 Laravel 的 __() 函数

标签 php laravel

我在 PHP 数组中有一组翻译。使用 Oscarotero 的 gettext 库,我收到错误:

"Cannot redeclare __() (previously declared in D:\LocaleTesting\vendor\laravel\framework\src\Illuminate\Foundation\helpers.php:907)" when the execution of the code $t->register()

 $aTranslation = Translations::fromJsonFile(public_path() . '/locale/'.$sLocale.'/LC_MESSAGES/admin.json');
 $oTranslator = new Translator();
 $oTranslator->loadTranslations($aTranslation);
 $oTranslator->register();

此外,我搜索到此错误仅在您的 Laravel 版本为 5.4 及以上版本时才会发生。任何帮助都可以。谢谢你!请Oscarotero/gettext的 github 以获取有关该库的更多信息。

最佳答案

这似乎是库的一个已知问题: https://github.com/oscarotero/Gettext/issues/180

解决这个问题的一种方法是在加载 laravel 助手之前加载翻译器函数(上述问题的解决方案):

I'm using the package with a require before vendor/autoload.php on public/index.php and artisan command.

# public/index.php
require __DIR__.'/../vendor/gettext/gettext/src/translator_functions.php';
require __DIR__.'/../vendor/autoload.php';
# artisan
require __DIR__.'/vendor/gettext/gettext/src/translator_functions.php';
require __DIR__.'/vendor/autoload.php';

关于php - 使用 oscarotero/gettext 翻译库时无法重新声明 Laravel 的 __() 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57156029/

相关文章:

php - Laravel 5.2 外键错误

php - 在 PHP 中使用 AES 加密时,应该在哪里存储 key ?

php - MySqli UPDATE,检测条目是否不存在

php - PHP 中的嵌套 MySQL 查询

unit-testing - 未找到类 'Mockery'

php - 如何创建模式表数据类型 longtext?

laravel - 在 Laravel Eloquent 中,如何检查一个 "has"中是否存在多个关系?

javascript - Highcharts 的 json_encode 输出格式

php - CodeIgniter 上传错误不显示

php - 如何知道更新了哪些字段