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

Plural in NativeArray?

Is it possible in some near (or far, irrelevant) future?



98.9k

Do you have in mind some suggested syntax?

edited Apr '14

Here are my thoughts (before this, I did not face, so only thoughts =)):

Translation file:

$messages = array (
'text_hello' => 'Hello, this is text',
'%apple% apple' => array('%apple% apple', '%apple% apples', '%apple% apples')
);

PHP code:

/**
* public function query($index, $placeholders=null, $isPlural=false){ }
**/

$translate = new \Phalcon\Translate\Adapter\NativeArray(array(
                "content" => $messages
            ));

echo $translate->query('%apple% apple', array('apple'=>3), true);

Look at plural of ZF style - https://github.com/zendframework/zf1/blob/master/library/Zend/Translate/Plural.php . Maybe we must have a function that will set this condition, for example:

$translate = new \Phalcon\Translate\Adapter\NativeArray(array(
                "content" => $messages,
                "plural" => function($number){
                       return (($number % 10 == 1) && ($number % 100 != 11)) ? 0 : ((($number % 10 >= 2) && ($number % 10 <= 4) && (($number % 100 < 10) || ($number % 100 >= 20))) ? 1 : 2); // this returns plural ID (plural_1, plural_2, etc)
                }
            ));

P.S. Btw, i think that sprintf style of placeholders is better (just example for placeholders, don't think about plural):

echo $translate->query('I have %s apple %d hours ago', 3, 15); 


98.9k

Ivan, could you please open an NFR on Github with this?

What about intl and MessageFormat? Plurals are handled based on a message and locale, so we retain a single translation method. Re Locale there were talks regarding integrating intl with Phalcon.