Add digit transformation function Language::formatNum() for Arabic and other
authorBrion Vibber <brion@users.mediawiki.org>
Sat, 6 Mar 2004 03:03:14 +0000 (03:03 +0000)
committerBrion Vibber <brion@users.mediawiki.org>
Sat, 6 Mar 2004 03:03:14 +0000 (03:03 +0000)
languages which need to use localized (decimal) digits. Sample implementation
for Arabic.

12 files changed:
includes/GlobalFunctions.php
includes/Skin.php
includes/SpecialImagelist.php
includes/SpecialLongpages.php
includes/SpecialNewpages.php
includes/SpecialPopularpages.php
includes/SpecialRecentchanges.php
includes/SpecialShortpages.php
includes/SpecialStatistics.php
includes/SpecialWatchlist.php
languages/Language.php
languages/LanguageAr.php

index bf95d2a..8e69807 100644 (file)
@@ -596,19 +596,22 @@ function wfRecordUpload( $name, $oldver, $size, $desc )
 
 function wfShowingResults( $offset, $limit )
 {
-       return wfMsg( "showingresults", $limit, $offset+1 );
+       global $wgLang;
+       return wfMsg( "showingresults", $wgLang->formatNum( $limit ), $wgLang->formatNum( $offset+1 ) );
 }
 
 function wfShowingResultsNum( $offset, $limit, $num )
 {
-       return wfMsg( "showingresultsnum", $limit, $offset+1, $num );
+       global $wgLang;
+       return wfMsg( "showingresultsnum", $wgLang->formatNum( $limit ), $wgLang->formatNum( $offset+1 ), $wgLang->formatNum( $num ) );
 }
 
 function wfViewPrevNext( $offset, $limit, $link, $query = "", $atend = false )
 {
-       global $wgUser;
-       $prev = wfMsg( "prevn", $limit );
-       $next = wfMsg( "nextn", $limit );
+       global $wgUser, $wgLang;
+       $fmtLimit = $wgLang->formatNum( $limit );
+       $prev = wfMsg( "prevn", $fmtLimit );
+       $next = wfMsg( "nextn", $fmtLimit );
        $link = wfUrlencode( $link );
 
        $sk = $wgUser->getSkin();
@@ -640,12 +643,13 @@ function wfViewPrevNext( $offset, $limit, $link, $query = "", $atend = false )
 
 function wfNumLink( $offset, $limit, $link, $query = "" )
 {
-       global $wgUser;
+       global $wgUser, $wgLang;
        if ( "" == $query ) { $q = ""; }
        else { $q = "{$query}&"; }
        $q .= "limit={$limit}&offset={$offset}";
 
-       $s = "<a href=\"" . wfLocalUrlE( $link, $q ) . "\">{$limit}</a>";
+       $fmtLimit = $wgLang->formatNum( $limit );
+       $s = "<a href=\"" . wfLocalUrlE( $link, $q ) . "\">{$fmtLimit}</a>";
        return $s;
 }
 
index 28d60a6..bbdeaad 100644 (file)
@@ -668,7 +668,7 @@ class Skin {
                if ( $wgDisableCounters ) {
                        $s = "";
                } else {
-                       $count = $wgArticle->getCount();
+                       $count = $wgLang->formatNum( $wgArticle->getCount() );
                        $s = wfMsg( "viewcount", $count );
                }
                $s .= $this->lastModified();
index d108036..a4e6bf5 100644 (file)
@@ -36,7 +36,7 @@ function wfSpecialImagelist()
        if ( 0 == $limit ) {
                $lt = wfMsg( "all" );
        } else {
-               $lt = "${limit}";
+               $lt = $wgLang->formatNum( "${limit}" );
                $sql .= " LIMIT {$limit}";
        }
        $wgOut->addHTML( "<p>" . wfMsg( "imglegend" ) . "\n" );
@@ -65,7 +65,7 @@ function wfSpecialImagelist()
                if ( ! $first ) { $fill .= " | "; }
                $first = false;
 
-               $fill .= $sk->makeKnownLink( $here, "{$num}",
+               $fill .= $sk->makeKnownLink( $here, $wgLang->formatNum( $num ),
                  "sort=bysize&limit={$num}" );
        }
        $text = wfMsg( "showlast", $fill, $bysize );
@@ -77,7 +77,7 @@ function wfSpecialImagelist()
                if ( ! $first ) { $fill .= " | "; }
                $first = false;
 
-               $fill .= $sk->makeKnownLink( $here, $num,
+               $fill .= $sk->makeKnownLink( $here, $wgLang->formatNum( $num ),
                  "sort=bydate&limit={$num}" );
        }
        $text = wfMsg( "showlast", $fill, $bydate );
@@ -94,7 +94,7 @@ function wfSpecialImagelist()
                $ilink = "<a href=\"" . wfImageUrl( $name ) .
                  "\">{$name}</a>";
 
-               $nb = wfMsg( "nbytes", $s->img_size );
+               $nb = wfMsg( "nbytes", $wgLang->formatNum( $s->img_size ) );
                $l = "(" .
                  $sk->makeKnownLink( $wgLang->getNsText(
                  Namespace::getImage() ) . ":{$name}", wfMsg( "imgdesc" ) ) .
index 2a9a1f5..403801d 100644 (file)
@@ -19,7 +19,8 @@ class LongPagesPage extends QueryPage {
        }
 
        function formatResult( $skin, $result ) {
-               $nb = wfMsg( "nbytes", $result->len );
+               global $wgLang;
+               $nb = wfMsg( "nbytes", $wgLang->formatNum( $result->len ) );
                $link = $skin->makeKnownLink( $result->cur_title, "" );
                return "{$link} ({$nb})";
        }
index ecd0e62..f0dfa25 100644 (file)
@@ -20,13 +20,12 @@ class NewPagesPage extends QueryPage {
        }
 
        function formatResult( $skin, $result ) {
-
                global $wgLang;
 
                $u = $result->cur_user;
                $ut = $result->cur_user_text;
 
-               $length = wfmsg( "nbytes", $result->cur_length );
+               $length = wfmsg( "nbytes", $wgLang->formatNum( $result->cur_length ) );
                $c = wfEscapeHTML( $result->cur_comment );
 
                if ( 0 == $u ) { # not by a logged-in user
index 8522b12..18dbb0d 100644 (file)
@@ -19,8 +19,9 @@ class PopularPagesPage extends QueryPage {
        }
 
        function formatResult( $skin, $result ) {
+               global $wgLang;
                $link = $skin->makeKnownLink( $result->cur_title, "" );
-               $nv = wfMsg( "nviews", $result->cur_counter );
+               $nv = wfMsg( "nviews", $wgLang->formatNum( $result->cur_counter ) );
                return "{$link} ({$nv})";
        }
 }
index 0ffba3c..c356d1e 100644 (file)
@@ -104,10 +104,10 @@ function wfSpecialRecentchanges( $par )
        }
 
        if(isset($from)) {
-               $note = wfMsg( "rcnotefrom", $limit,
+               $note = wfMsg( "rcnotefrom", $wgLang->formatNum( $limit ),
                        $wgLang->timeanddate( $from, true ) );
        } else {
-               $note = wfMsg( "rcnote", $limit, $days );
+               $note = wfMsg( "rcnote", $wgLang->formatNum( $limit ), $wgLang->formatNum( $days ) );
        }
        $wgOut->addHTML( "\n<hr>\n{$note}\n<br>" );
 
@@ -142,7 +142,7 @@ function rcCountLink( $lim, $d, $page="Recentchanges", $more="" )
        global $wgUser, $wgLang;
        $sk = $wgUser->getSkin();
        $s = $sk->makeKnownLink( $wgLang->specialPage( $page ),
-         ($lim ? "{$lim}" : wfMsg( "all" ) ), "{$more}" .
+         ($lim ? $wgLang->formatNum( "{$lim}" ) : wfMsg( "all" ) ), "{$more}" .
          ($d ? "days={$d}&" : "") . "limit={$lim}" );
        return $s;
 }
@@ -152,7 +152,7 @@ function rcDaysLink( $lim, $d, $page="Recentchanges", $more="" )
        global $wgUser, $wgLang;
        $sk = $wgUser->getSkin();
        $s = $sk->makeKnownLink( $wgLang->specialPage( $page ),
-         ($d ? "{$d}" : wfMsg( "all" ) ), "{$more}days={$d}" .
+         ($d ? $wgLang->formatNum( "{$d}" ) : wfMsg( "all" ) ), "{$more}days={$d}" .
          ($lim ? "&limit={$lim}" : "") );
        return $s;
 }
index 2cb6c37..b989277 100644 (file)
@@ -19,7 +19,8 @@ class ShortPagesPage extends QueryPage {
        }
 
        function formatResult( $skin, $result ) {
-               $nb = wfMsg( "nbytes", $result->len );
+               global $wgLang;
+               $nb = wfMsg( "nbytes", $wgLang->formatNum( $result->len ) );
                $link = $skin->makeKnownLink( $result->cur_title, "" );
                return "{$link} ({$nb})";
        }
index f28882a..1a96dcf 100644 (file)
@@ -2,7 +2,7 @@
 
 function wfSpecialStatistics()
 {
-       global $wgUser, $wgOut;
+       global $wgUser, $wgOut, $wgLang;
        $fname = "wfSpecialStatistics";
 
        $wgOut->addHTML( "<h2>" . wfMsg( "sitestats" ) . "</h2>\n" );
@@ -21,9 +21,12 @@ function wfSpecialStatistics()
        $good = $row->ss_good_articles;
 
        $text = wfMsg( "sitestatstext",
-               $total, $good, $views, $edits,
-               sprintf( "%.2f", $total ? $edits / $total : 0 ),
-               sprintf( "%.2f", $edits ? $views / $edits : 0 ) );
+               $wgLang->formatNum( $total ),
+               $wgLang->formatNum( $good ),
+               $wgLang->formatNum( $views ),
+               $wgLang->formatNum( $edits ),
+               $wgLang->formatNum( sprintf( "%.2f", $total ? $edits / $total : 0 ) ),
+               $wgLang->formatNum( sprintf( "%.2f", $edits ? $views / $edits : 0 ) ) );
 
        $wgOut->addHTML( $text );
        $wgOut->addHTML( "<h2>" . wfMsg( "userstats" ) . "</h2>\n" );
@@ -42,7 +45,9 @@ function wfSpecialStatistics()
        $sk = $wgUser->getSkin();
        $ap = $sk->makeKnownLink( wfMsg( "administrators" ), "" );
 
-       $text = wfMsg( "userstatstext", $total, $admins, $ap );
+       $text = wfMsg( "userstatstext",
+               $wgLang->formatNum( $total ),
+               $wgLang->formatNum( $admins ), $ap );
        $wgOut->addHTML( $text );
 }
 
index a763e8b..3dd8806 100644 (file)
@@ -89,7 +89,7 @@ function wfSpecialWatchlist()
        }
        
        if(isset($_REQUEST['magic'])) {
-               $wgOut->addHTML( wfMsg( "watchlistcontains", $nitems ) .
+               $wgOut->addHTML( wfMsg( "watchlistcontains", $wgLang->formatNum( $nitems ) ) .
                        "<p>" . wfMsg( "watcheditlist" ) . "</p>\n" );
                
                $wgOut->addHTML( "<form action='" .
@@ -133,7 +133,8 @@ function wfSpecialWatchlist()
        }
 
        
-       $wgOut->addHTML( "<i>" . wfMsg( "watchdetails", $nitems, $npages, $y,
+       $wgOut->addHTML( "<i>" . wfMsg( "watchdetails",
+               $wgLang->formatNum( $nitems ), $wgLang->formatNum( $npages ), $y,
                $specialTitle->getURL( "magic=yes", true ) ) . "</i><br>\n" );
         
 
@@ -151,9 +152,9 @@ function wfSpecialWatchlist()
        $res = wfQuery( $sql, DB_READ, $fname );
 
        if($days >= 1)
-               $note = wfMsg( "rcnote", $limit, $days );
+               $note = wfMsg( "rcnote", $wgLang->formatNum( $limit ), $wgLang->formatNum( $days ) );
        elseif($days > 0)
-               $note = wfMsg( "wlnote", $limit, round($days*24) );
+               $note = wfMsg( "wlnote", $wgLang->formatNum( $limit ), $wgLang->formatNum( round($days*24) ) );
        else
                $note = "";
        $wgOut->addHTML( "\n<hr>\n{$note}\n<br>" );
@@ -189,7 +190,8 @@ function wlHoursLink( $h, $page ) {
        $sk = $wgUser->getSkin();
        $s = $sk->makeKnownLink(
          $wgLang->specialPage( $page ),
-         $h, "days=" . ($h / 24.0) );
+         $wgLang->formatNum( $h ),
+         "days=" . ($h / 24.0) );
        return $s;
 }
 
@@ -199,7 +201,7 @@ function wlDaysLink( $d, $page ) {
        $sk = $wgUser->getSkin();
        $s = $sk->makeKnownLink(
          $wgLang->specialPage( $page ),
-         ($d ? $d : wfMsg( "all" ) ), "days=$d" );
+         ($d ? $wgLang->formatNum( $d ) : wfMsg( "all" ) ), "days=$d" );
        return $s;
 }
 
index 46211f3..be6bdad 100644 (file)
@@ -1560,23 +1560,15 @@ class Language {
                        $datePreference = $wgAmericanDates ? 1 : 2;
                }
                
-               if ( $datePreference == 1 ) {
-                       # MDY
-                       $d = $this->getMonthAbbreviation( substr( $ts, 4, 2 ) ) .
-                         " " . (0 + substr( $ts, 6, 2 )) . ", " .
-                         substr( $ts, 0, 4 );
-               } else if ( $datePreference == 2 ) {
-                       #DMY
-                       $d = (0 + substr( $ts, 6, 2 )) . " " .
-                         $this->getMonthAbbreviation( substr( $ts, 4, 2 ) ) . " " .
-                         substr( $ts, 0, 4 );
-               } else {
-                       #YMD
-                       $d = substr( $ts, 0, 4 ) . " " . $this->getMonthAbbreviation( substr( $ts, 4, 2 ) ) .
-                               " " . (0 + substr( $ts, 6, 2 ));
+               $month = $this->getMonthAbbreviation( substr( $ts, 4, 2 ) );
+               $day = $this->formatNum( 0 + substr( $ts, 6, 2 ) );
+               $year = $this->formatNum( substr( $ts, 0, 4 ) );
+               
+               switch( $datePreference ) {
+                       case 1: return "$month $day, $year";
+                       case 2: return "$day $month $year";
+                       default: return "$year $month $day";
                }
-
-               return $d;
        }
 
        function time( $ts, $adj = false )
@@ -1584,7 +1576,7 @@ class Language {
                if ( $adj ) { $ts = $this->userAdjust( $ts ); }
 
                $t = substr( $ts, 8, 2 ) . ":" . substr( $ts, 10, 2 );
-               return $t;
+               return $this->formatNum( $t );
        }
 
        function timeanddate( $ts, $adj = false )
@@ -1748,6 +1740,13 @@ class Language {
        {
                return NULL;
        }
+       
+       # Normally we use the plain ASCII digits. Some languages such as Arabic will
+       # want to output numbers using script-appropriate characters: override this
+       # function with a translator. See LanguageAr.php for an example.
+       function formatNum( $number ) {
+               return $number;
+       }
 
 }
 
index 6eae649..ca54f04 100644 (file)
@@ -3,18 +3,18 @@
 include_once("LanguageUtf8.php");
 
 /* private */ $wgNamespaceNamesAr = array(
-       "-2" => "ملف",
-       "-1" => "خاص",
-       "0" => "",
-       "1" => "نقاش",
-       "2" => "مستخدم",
-       "3" => "نقاش_المستخدم",
-       "4" => "ويكيبيديا",
-       "5" => "ويكيبيديا_نقاش",
-       "6" => "صورة",
-       "7" => "نقاش_الصورة",
-       "8" => "MediaWiki",
-       "9" => "MediaWiki_talk",
+       -2 => "ملف",
+       -1 => "خاص",
+       0 => "",
+       1 => "نقاش",
+       2 => "مستخدم",
+       3 => "نقاش_المستخدم",
+       4 => "ويكيبيديا",
+       5 => "ويكيبيديا_نقاش",
+       6 => "صورة",
+       7 => "نقاش_الصورة",
+       8 => "MediaWiki",
+       9 => "MediaWiki_talk",
 );
 
 /* private */ $wgWeekdayNamesAr = array(
@@ -29,6 +29,22 @@ include_once("LanguageUtf8.php");
 );
 
 class LanguageAr extends LanguageUtf8 {
+       var $digitTransTable = array(
+               "0" => "٠",
+               "1" => "١",
+               "2" => "٢",
+               "3" => "٣",
+               "4" => "٤",
+               "5" => "٥",
+               "6" => "٦",
+               "7" => "٧",
+               "8" => "٨",
+               "9" => "٩",
+               "%" => "٪",
+               "." => "٫",
+               "," => "٬"
+       );
+       
        # TODO: TRANSLATION!
 
        # Inherit everything except...
@@ -101,7 +117,10 @@ class LanguageAr extends LanguageUtf8 {
                
                return $s;
        }
-
+       
+       function formatNum( $number ) {
+               return strtr( $number, $this->digitTransTable );
+       }
 }
 
 ?>