php-gettext / Gettext

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

Call to a member function insert() on boolean #228

Closed mihaikelemen closed 4 years ago

mihaikelemen commented 4 years ago

Scanning PHP code fails on v4.7 for the this type of translation __("Foo %s", "bar")

This is working on v4.6.3

PHP Fatal error:  Uncaught Error: Call to a member function insert() on boolean in /vendor/gettext/gettext/src/Utils/FunctionsScanner.php:77
Stack trace:
#0 /vendor/gettext/gettext/src/Extractors/PhpCode.php(68): Gettext\Utils\FunctionsScanner->saveGettextFunctions(Array, Array)
#1 /vendor/gettext/gettext/src/Extractors/PhpCode.php(51): Gettext\Extractors\PhpCode::fromStringMultiple('<?php\nnamespace...', Array, Array)
#2 /vendor/gettext/gettext/src/Extractors/Extractor.php(18): Gettext\Extractors\PhpCode::fromString('<?php\nnamespace...', Object(Gettext\Translations), Array)
#3 [internal function]: Gettext\Extractors\Extractor::fromFile('/file_path/en_US/messages...', Object(Gettext\Translations), Array)
#4 /vendor/gettext/gettext/src/Translations.php(174): call_user_func('Gettext\\Extract...', '/file_path/en_US/messages...', Object(Gettext\Translations), Array)
#5 file_path: Gettext\Translations->__call( in /vendor/gettext/gettext/src/Utils/FunctionsScanner.php on line 77
oscarotero commented 4 years ago

Ok, I guess this is a bug introduced in #223 @briedis could you please fix this? I don't have free time these days.

briedis commented 4 years ago

I'll check it out

briedis commented 4 years ago

@mihaikelemen Could you provide more info?

  1. Can you give me a code example that is failing?
  2. What is the __ function, why does it have two arguments in your example? Is that a domain, context method?
  3. Can you show the function configuration - which function is considered which?

By default, __ should have only one argument: image

mihaikelemen commented 4 years ago

Hi @briedis ,

Sorry for the late response. I'm using this approach with the second argument for quite a while, based on the docs (see bellow). It is a handful approach.

Selecție_20191018

Mihai

mihaikelemen commented 4 years ago

This the actual code (part of it) that generates the po file for a given locale (in this case ro_RO)

$files->files()
->name('/(.*)\.(tpl|php)/')
->depth('>=1')
->in($dirs);

$translations = new Translations();
$translations->setLanguage('ro_RO');
$translations->setDomain('messages');

# locale directory path
$dir = sprintf('%s%s/', $config->get('environment.locales'), 'ro_RO');
$potfile = sprintf('%s%s.po', $dir, $translations->getDomain());

if (!$filesystem->exists($dir)) {
    $filesystem->mkdir($dir, 0755);
    $filesystem->touch($potfile);
}

foreach ($files as $file) {
    $translations->addFromPhpCodeFile($file->getPathname());
}

if ($filesystem->exists($potfile)) {
    $translations->mergeWith(Translations::fromPoFile($potfile));
}

$translations->toPoFile($potfile);
briedis commented 4 years ago

Sorry @mihaikelemen for the delay, but I managed to run into this issue on my own too. A PR is created for the v4 branch. Actually the code to reproduce the issue was much simpler - just scan a file for one domain, but the file contains a single pgettext call without any strings from the domain you are searching for.

mihaikelemen commented 4 years ago

Hi @briedis , Thanks for your response. For now I'm using 4.6.3 until otherwise; eventually I will upgrade my code to run with the newer v5 branch.

Mihai