NumberFormatter::SPELLOUT spellout-ordinal in russian and italian
Asked Answered
B

2

11

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");  
Bradstreet answered 18/6, 2014 at 9:47 Comment(11)
For me calling format(42) result is (in order): "сорок два", "cuarenta y dos" and "quaranta ­due". Is this the expected output? Make sure you have intl installed.Augustus
the ordinal number for 42 called "сорок второй", in spainish "cuadragésimo segundo", in italian "quarantaduesimoBradstreet
yeah i tested it $ru_ordinal->format(42); returns сорок два instead of сорок второй. this is weirdSanfordsanfourd
@Sanfordsanfourd It all works fine for me! I think you don't want the number as ordinal number alone, but the number as date like: it's the second day and you don't want: It's the two day right?Kutenai
@Kutenai How does it show the ordinal number? I am searching for a solution only for the ordinal numbers for russian and italian.Bradstreet
@Bradstreet Do you want the ordinal number like: it's the second day in all languages or do you want it like it's the two day ? Like you don't want to use the number translated alone, you wanted it with a date or something like that?Kutenai
@Kutenai I want the ordinal number in russian and italian, in your example it would be second. I want to translate the number alone!Bradstreet
@Bradstreet Yes that means e.g. you DON't want 2 -> два you want: 2 -> второй right? and right now you get the first one?Kutenai
@Kutenai Exactly an in italian I get 2 -> due instead of 2 -> secondo.Bradstreet
Let us continue this discussion in chat.Bradstreet
@Bradstreet No i just wanted to make it clear what you want, now i got it :D I think you should add that to you question and show what exactly you want!Kutenai
F
12

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

Frazzled answered 10/1, 2015 at 22:40 Comment(8)
Thank you. Unfortunately my hoster only update recently to PHP 5.4 and ICU 4.4.1. I have checked your code on phpfiddle.org and here I get the cardinal number instead the ordinal number.Bradstreet
Phpfiddle uses PHP 5.3.29. I tested this on 5.4.12. I added some addtional references. I think that ends the topic and in fact there is no other answer for now so I hope you will mark it as accepted. Best wishes!Frazzled
Thanks again, but I already know the addtional references. It also don't work on 5.4.36-1. This PHP version is used on my website.Bradstreet
Here you go, working example on my server: private.arius.pl/test/example.php?number=12 - you can change number like you want. Update your servers locale to have correct values for italian.Frazzled
Can you show me the code for example.php like example.phps ? I don't know, if I can change my server locale. I don't use my own server. Does ist also works with %spellout-ordinal-maskuline instead of %spellout-ordinal-feminine ?Bradstreet
Basically there is the same code as I pasted in the answer (only echoed and added phpversion() call). I checked most of php fiddles and except one they have the same problem (there is cardinal instead ordinal). So propably this is quite common misconfiguration for hosting services :(Frazzled
Let us continue this discussion in chat.Bradstreet
spellout-ordinal-masculine for russian work great. But wrong on ukrainian, have any idea? ICU: 64.2Guinevere
O
2

Just a Recommendation, I am not sure if this works or have an Apache services open at this point of time as I am at college, but have you tried to put ru-RU for Russia. In PHP I personally put my Language Codes as "en-GB"

http://download1.parallels.com/SiteBuilder/Windows/docs/3.2/en_US/sitebulder-3.2-win-sdk-localization-pack-creation-guide/30801.htm

Here is a List I found on the internet with some to help you.

Oakum answered 18/6, 2014 at 9:58 Comment(1)
Thank you, but I had also tried ru-RU and fr-FR for french, es-ES for spanish, it-IT for italian, it works only english and german.Bradstreet

© 2022 - 2024 — McMap. All rights reserved.