X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=include%2Fcommon.pm;h=207bfe9297d836b2e7a8fa979ad1bf1725dfbe6e;hb=2d2a3a01f1d6828f37de15bd52a7bae6d64da117;hp=7ffaeeee0e8fc7ab4cae055559e7621a81ef3675;hpb=0b296666f77d2431a26be5be4856499c5e0c2cd8;p=wloh diff --git a/include/common.pm b/include/common.pm index 7ffaeee..207bfe9 100644 --- a/include/common.pm +++ b/include/common.pm @@ -135,4 +135,43 @@ 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"; + } +} + +sub find_all_locales { + my $dbh = shift; + my $q = $dbh->prepare('SELECT kultur FROM fotballspraak WHERE nyestesesong<>-1'); + $q->execute; + + my @locales = (); + while (my $ref = $q->fetchrow_hashref) { + push @locales, $ref->{'kultur'}; + } + + return @locales; +} + 1;