From da7fdbfd2feb8c602855322027d1312e446232e3 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Sun, 10 Jun 2012 11:37:02 +0200 Subject: [PATCH] Support per-language ordinal styles. --- include/common.pm | 26 ++++++++++++++++++++++++++ www/index.pl | 6 +++--- www/rating.pl | 4 ++-- 3 files changed, 31 insertions(+), 5 deletions(-) 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; diff --git a/www/index.pl b/www/index.pl index efa5517..eb6d176 100755 --- a/www/index.pl +++ b/www/index.pl @@ -203,7 +203,7 @@ sub make_table { # Make list of ranks. (Relegation is handled specially.) my @ranks = (); for my $i (1..$num_games) { - push @ranks, { 'th' => "$i." }; + push @ranks, { 'th' => wloh_common::get_ordinal($i, $locale) }; } my @players = (); @@ -380,13 +380,13 @@ if (defined($match_player) && defined($match_position)) { print CGI->header(-type=>'text/html; charset=utf-8', -expires=>'+5m'); wloh_common::process_template('scenario-not-found', $locale, { '#nick' => $player_name, - '#rank' => sprintf("%d.", $match_position + 1) + '#rank' => wloh_common::get_ordinal($match_position + 1, $locale) }); } else { print CGI->header(-type=>'text/html; charset=utf-8', -expires=>'+5m'); wloh_common::process_template('scenario', $locale, { '#nick' => $player_name, - '#rank' => sprintf("%d.", $match_position + 1), + '#rank' => wloh_common::get_ordinal($match_position + 1, $locale), '#results' => \@scenario }); } diff --git a/www/rating.pl b/www/rating.pl index dca62d8..fa1eb9d 100755 --- a/www/rating.pl +++ b/www/rating.pl @@ -52,7 +52,7 @@ my $i = 0; while (my $ref = $q->fetchrow_hashref) { my %division = (); - $division{'#rank'} = sprintf "%d.", $ref->{'divisjon'}; + $division{'#rank'} = wloh_common::get_ordinal($ref->{'divisjon'}, $locale); $division{'#average'} = sprintf "%.1f", $ref->{'avg_rating'}; $division{'#stddev'} = sprintf "%.1f", $ref->{'stddev_rating'}; @@ -86,7 +86,7 @@ $i = 0; while (my $ref = $q->fetchrow_hashref) { my %player = (); - $player{'#rank'} = sprintf "%d.", ++$i; + $player{'#rank'} = wloh_common::get_ordinal(++$i, $locale); $player{'#user'} = Encode::decode_utf8($ref->{'navn'}); $player{'#user/href'} = sprintf "http://wordfeud.aasmul.net/bruker-%d", $ref->{'id'}; $player{'#rating'} = sprintf "%.1f", $ref->{'rating'}; -- 2.39.2