php-gettext / Gettext

PHP library to collect and manipulate gettext (.po, .mo, .php, .json, etc)
MIT License
687 stars 134 forks source link

Duplicate function name with Laravel #180

Open borys-p opened 6 years ago

borys-p commented 6 years ago

Hi,

It seems that both Gettext and Laravel are defining __() function. Although Laravel wraps their definition with function_exists(), it's not normally possible to register Gettext's functions before Laravel's helpers.php is loaded, as it's automatically included with Composer.

The only solution I've found so far (other than messing in vendor files) is https://github.com/funkjedi/composer-include-files.

oscarotero commented 6 years ago

Thanks for the info. Maybe @eusonlito (creator of the laravel-Gettext package) knows other workaround

eusonlito commented 6 years ago

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

Regards, Lito.

borys-p commented 6 years ago

Yes, that's another solution. But it's... inelegant. You make a strong coupling with a vendor library, while Laravel recently introduced auto-discovery feature to reduce this.

To be honest, the Composer hack isn't much better, but in theory Gettext could control it. If only there was a way to force Composer to include translator_functions.php before Laravel's helpers then it would fix this issue (I assume that if you include Gettext into Laravel, then you don't care about the built-in __() function).

eusonlito commented 6 years ago

Is not the best way to do it, but it's almost clear.

Laravel __ is loaded as helper function after check if not exists: https://github.com/laravel/framework/blob/4c9915c666c9cc085f5c749d640bcf0d309a64a5/src/Illuminate/Foundation/helpers.php#L926