X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=include%2Fcommon.pm;fp=include%2Fcommon.pm;h=9348b15da9a10bb67736234c8ad465d575c5733c;hb=da7fdbfd2feb8c602855322027d1312e446232e3;hp=7ffaeeee0e8fc7ab4cae055559e7621a81ef3675;hpb=ec88f67e841ab19faaac402bf981237b0e101c8e;p=wloh diff --git a/include/common.pm b/include/common.pm index 7ffaeee..9348b15 100644 --- a/include/common.pm +++ b/include/common.pm @@ -135,4 +135,30 @@ sub process_template { print $doc->toString; } +sub get_ordinal { + my ($num, $locale) = @_; + my $translation = $translation_mapping{$locale} // 'en'; + + if ($translation eq 'no') { + return $num . "."; + } elsif ($translation eq 'en') { + # http://en.wikipedia.org/wiki/English_numerals#Ordinal_numbers + my $units = $num % 10; + my $tens = (($num - $units) / 10) % 10; + if ($tens == 1) { + return $num . "th"; + } elsif ($units == 1) { + return $num . "st"; + } elsif ($units == 2) { + return $num . "nd"; + } elsif ($units == 3) { + return $num . "rd"; + } else { + return $num . "th"; + } + } else { + die "Missing ordinal strategy for locale $locale"; + } +} + 1;