]> git.sesse.net Git - wloh/commitdiff
Support per-language ordinal styles.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Sun, 10 Jun 2012 09:37:02 +0000 (11:37 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Sun, 10 Jun 2012 09:37:02 +0000 (11:37 +0200)
include/common.pm
www/index.pl
www/rating.pl

index 7ffaeeee0e8fc7ab4cae055559e7621a81ef3675..9348b15da9a10bb67736234c8ad465d575c5733c 100644 (file)
@@ -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;
index efa55175bd828ef927a485e0d6172004fc138435..eb6d1761f613376d16bc4dd9dc98d07043337621 100755 (executable)
@@ -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
                });
        }
index dca62d89b527d0a305ded73cf00950282a5ea337..fa1eb9d9fda943eacaa1f91521fa7f5908b2bca8 100755 (executable)
@@ -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'};