We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Extending Phalcon Gettext Class

I've run into an issue using the plural version of gettext (nquery) in my project. The issue is that when I run xgettext to scan for translations, it does not recognise the phalcon nquery function as being a valid translation function and therefore does not add the correct pluralisation syntax to the .po file.

So for example the following will translate, however is not seen by xgettext

echo sprintf($this->locale->nquery('There is 1 cat.', 'There are %d cats.', 4),4);

If I structure the syntax using the original gettext call, the pluralisations are correctly added to the .po file, however it obviously does not translate as ngettext is not a function in the phalcon gettext adapter class.

echo sprintf($this->locale->ngettext('There is 1 cat.', 'There are %d cats.', 4),4);

My first thought was, can I extend the Phalcon Gettext translate adapter class to create an alias of ngettext to nquery?

Here is my attempt

<?php

namespace App;

use Phalcon\Translate\Adapter\Gettext;

class Apptext extends Gettext
{

    public function __construct(array $options)
    {
        parent::__construct($options);
    }

    public function ngettext($msgid1, $msgid2, $count, $placeholders = null, $domain = null) {
        return $this->nquery($msgid1, $msgid2, $count, $placeholders, $domain);
    }

    /**
     * Gets default options
     *
     * @return array
     */
    private function getOptionsDefault() {}

}

Unfortunately, I haven't had any luck.

With the above class I get the following error:

Warning: First argument is not an array in /var/www/app/library/Apptext.php on line 12

Notice: Cannot use a scalar value as an array in phalcon/translate/adapter/gettext.zep on line 229 in /var/www/app/library/Apptext.php on line 12

Notice: Cannot use a scalar value as an array in phalcon/translate/adapter/gettext.zep on line 229 in /var/www/app/library/Apptext.php on line 12
Error

Parameter 'category' must be a int

If I var_dump line 12 in Apptext.php I can see that it is an array, and that category is an integer.

array(4) { ["locale"]=> string(5) "en_US" ["directory"]=> string(29) "/var/www/app/locale" ["domain"]=> string(8) "messages" ["category"]=> int(5) }

I've trawled the web for some examples of extending Phalcon classes like this, but surprisingly I haven't found much.

I'd welcome any suggestions on getting the gettext class extended, or even suggesting a different approach to the original issue of xgettext not scanning the Phalcon 'nquery' pluralisation function.

Looks like it's not about phalcon, more like zephir bug and that's it causing this error in phalcon.