Search code examples
phpicuintlnumberformatter

NumberFormatter::SPELLOUT spellout-ordinal in russian and italian


this code works for english, spanish and german ordninal numbers, but with russian or italian ordninal numbers it doesn't work.

'ru-RU','it-IT' also don't work

I get for example in russian for 2 -> два (this is the cardinal number) , but I want the ordinal number and this would be here 2 -> второй.

I get for example in italian for 2 -> due (this is the cardinal number) , but I want the ordinal number and this would be here 2 -> secondo.

Update:

I found a solution with works in french, spain, german and some other languages:

maskuline ordinal numbers: %spellout-ordinal-maskuline

feminine ordinal numbers: %spellout-ordinal-feminine

russian and italian version doesn't work and I tried already with -maskuline/-feminine

$ru_ordinal = new NumberFormatter('ru', NumberFormatter::SPELLOUT);
$ru_ordinal->setTextAttribute(NumberFormatter::DEFAULT_RULESET, "%spellout-ordinal");  

Solution

  • NumberFormatter is using ICU formatting.

    As you can check here: http://saxonica.com/html/documentation/extensibility/config-extend/localizing/ICU-numbering-dates/ICU-numbering.html

    ... Russian (ru) has following formatting available:

    • spellout-cardinal-feminine (scf)
    • spellout-cardinal-masculine (scm)
    • spellout-cardinal-neuter (scne)
    • spellout-numbering (sn)
    • spellout-numbering-year (sny)

    ... and Italian (it):

    • spellout-cardinal-feminine (scf)
    • spellout-cardinal-masculine (scm)
    • spellout-numbering (sn)
    • spellout-numbering-year (sny)
    • spellout-ordinal-feminine (sof)
    • spellout-ordinal-masculine (som)

    That is why you will not be able to set ordinal format for (ru) and following code:

    $nFormat = new NumberFormatter('it', NumberFormatter::SPELLOUT);
    $nFormat->setTextAttribute(NumberFormatter::DEFAULT_RULESET, "%spellout-ordinal-feminine");
    
    var_dump($nFormat->format(42));
    

    Will print:

    string 'quaranta­duesima' (length=17)
    

    Like you (propably) want.

    EDIT:

    Informations about used formatting with references to ICU: http://php.net/manual/en/numberformatter.create.php

    Tested with PHP 5.4.x and ICU version => 51.2; ICU Data version => 51.2. You can use shell command:

    $ php -i | grep ICU
    

    To check what version of ICU you have.

    For latest ICU version you should propably install/update php-intl package: http://php.net/manual/en/intl.installation.php

    EDIT 2:

    I have created extension for NumberFormatter (so far with polish ordinals). Feel free to contribute another languages: https://github.com/arius86/number-formatter