Index: /branches/release-35/php/lib/function.mtentryscorecount.php
===================================================================
--- /branches/release-35/php/lib/function.mtentryscorecount.php (revision 1174)
+++ /branches/release-35/php/lib/function.mtentryscorecount.php (revision 1956)
@@ -9,5 +9,5 @@
 
 function smarty_function_mtentryscorecount($args, &$ctx) {
-    return hdlr_score_count($ctx, 'entry', $args['namespace']);
+    $count = hdlr_score_count($ctx, 'entry', $args['namespace']);
+    return $ctx->count_format($count, $args);
 }
-?>
Index: /branches/release-35/php/lib/function.mtblogentrycount.php
===================================================================
--- /branches/release-35/php/lib/function.mtblogentrycount.php (revision 1174)
+++ /branches/release-35/php/lib/function.mtblogentrycount.php (revision 1956)
@@ -11,5 +11,4 @@
     $args['blog_id'] = $ctx->stash('blog_id');
     $count = $ctx->mt->db->blog_entry_count($args);
-    return $count;
+    return $ctx->count_format($count, $args);
 }
-?>
Index: /branches/release-35/php/lib/function.mtblogcategorycount.php
===================================================================
--- /branches/release-35/php/lib/function.mtblogcategorycount.php (revision 1174)
+++ /branches/release-35/php/lib/function.mtblogcategorycount.php (revision 1956)
@@ -11,5 +11,4 @@
     $args['blog_id'] = $ctx->stash('blog_id');
     $count = $ctx->mt->db->blog_category_count($args);
-    return $count;
+    return $ctx->count_format($count, $args);
 }
-?>
Index: /branches/release-35/php/lib/function.mtauthorscorecount.php
===================================================================
--- /branches/release-35/php/lib/function.mtauthorscorecount.php (revision 1174)
+++ /branches/release-35/php/lib/function.mtauthorscorecount.php (revision 1956)
@@ -9,6 +9,5 @@
 
 function smarty_function_mtauthorscorecount($args, &$ctx) {
-    return hdlr_score_count($ctx, 'author', $args['namespace']);
+    $count = hdlr_score_count($ctx, 'author', $args['namespace']);
+    return $ctx->count_format($count, $args);
 }
-?>
-
Index: /branches/release-35/php/lib/function.mtassetscorecount.php
===================================================================
--- /branches/release-35/php/lib/function.mtassetscorecount.php (revision 1174)
+++ /branches/release-35/php/lib/function.mtassetscorecount.php (revision 1956)
@@ -9,6 +9,5 @@
 
 function smarty_function_mtassetscorecount($args, &$ctx) {
-    return hdlr_score_count($ctx, 'asset', $args['namespace']);
+    $count = hdlr_score_count($ctx, 'asset', $args['namespace']);
+    return $ctx->count_format($count, $args);
 }
-?>
-
Index: /branches/release-35/php/lib/function.mttagcount.php
===================================================================
--- /branches/release-35/php/lib/function.mttagcount.php (revision 1174)
+++ /branches/release-35/php/lib/function.mttagcount.php (revision 1956)
@@ -8,10 +8,7 @@
 function smarty_function_mttagcount($args, &$ctx) {
     $tag = $ctx->stash('Tag');
-    if (!$tag) return '';
-    if (is_array($tag)) {
-        return $tag['tag_count'];
-    } else {
-        return 0;
-    }
+    $count = 0;
+    if ($tag && is_array($tag))
+        $count = $tag['tag_count'];
+    return $ctx->count_format($count, $args);
 }
-?>
Index: /branches/release-35/php/lib/function.mtcommentscorecount.php
===================================================================
--- /branches/release-35/php/lib/function.mtcommentscorecount.php (revision 1174)
+++ /branches/release-35/php/lib/function.mtcommentscorecount.php (revision 1956)
@@ -9,5 +9,5 @@
 
 function smarty_function_mtcommentscorecount($args, &$ctx) {
-    return hdlr_score_count($ctx, 'comment', $args['namespace']);
+    $count = hdlr_score_count($ctx, 'comment', $args['namespace']);
+    return $ctx->count_format($count, $args);
 }
-?>
Index: /branches/release-35/php/lib/function.mtpingscorecount.php
===================================================================
--- /branches/release-35/php/lib/function.mtpingscorecount.php (revision 1174)
+++ /branches/release-35/php/lib/function.mtpingscorecount.php (revision 1956)
@@ -9,5 +9,5 @@
 
 function smarty_function_mtpingscorecount($args, &$ctx) {
-    return hdlr_score_count($ctx, 'tbping', $args['namespace']);
+    $count = hdlr_score_count($ctx, 'tbping', $args['namespace']);
+    return $ctx->count_format($count, $args);
 }
-?>
Index: /branches/release-35/php/lib/function.mtcategorycommentcount.php
===================================================================
--- /branches/release-35/php/lib/function.mtcategorycommentcount.php (revision 1174)
+++ /branches/release-35/php/lib/function.mtcategorycommentcount.php (revision 1956)
@@ -11,6 +11,8 @@
     $category = $ctx->stash('category');
     $cat_id = (int)$category['category_id'];
-    if (!$cat_id) return 0;
-    return $db->category_comment_count(array( 'category_id' => $cat_id ));
+    $count = 0;
+    if ($cat_id) {
+        $count = $db->category_comment_count(array( 'category_id' => $cat_id ));
+    }
+    return $ctx->count_format($count, $args);
 }
-?>
Index: /branches/release-35/php/lib/function.mtentrytrackbackcount.php
===================================================================
--- /branches/release-35/php/lib/function.mtentrytrackbackcount.php (revision 1583)
+++ /branches/release-35/php/lib/function.mtentrytrackbackcount.php (revision 1956)
@@ -8,5 +8,5 @@
 function smarty_function_mtentrytrackbackcount($args, &$ctx) {
     $entry = $ctx->stash('entry');
-    return $entry['entry_ping_count'];
+    $count = $entry['entry_ping_count'];
+    return $ctx->count_format($count, $args);
 }
-?>
Index: /branches/release-35/php/lib/function.mtblogpagecount.php
===================================================================
--- /branches/release-35/php/lib/function.mtblogpagecount.php (revision 1174)
+++ /branches/release-35/php/lib/function.mtblogpagecount.php (revision 1956)
@@ -13,3 +13,2 @@
     return smarty_function_mtblogentrycount($args, $ctx);
 }
-?>
Index: /branches/release-35/php/lib/function.mtcategorytrackbackcount.php
===================================================================
--- /branches/release-35/php/lib/function.mtcategorytrackbackcount.php (revision 1174)
+++ /branches/release-35/php/lib/function.mtcategorytrackbackcount.php (revision 1956)
@@ -9,5 +9,5 @@
     $cat = $ctx->stash('category');
     $cat_id = $cat['category_id'];
-    return $ctx->mt->db->category_ping_count($cat_id);
+    $count = $ctx->mt->db->category_ping_count($cat_id);
+    return $ctx->count_format($count, $args);
 }
-?>
Index: /branches/release-35/php/lib/function.mtblogpingcount.php
===================================================================
--- /branches/release-35/php/lib/function.mtblogpingcount.php (revision 1174)
+++ /branches/release-35/php/lib/function.mtblogpingcount.php (revision 1956)
@@ -8,5 +8,5 @@
 function smarty_function_mtblogpingcount($args, &$ctx) {
     $args['blog_id'] = $ctx->stash('blog_id');
-    return $ctx->mt->db->blog_ping_count($args);
+    $count = $ctx->mt->db->blog_ping_count($args);
+    return $ctx->count_format($count, $args);
 }
-?>
Index: /branches/release-35/php/lib/function.mtassetcount.php
===================================================================
--- /branches/release-35/php/lib/function.mtassetcount.php (revision 1174)
+++ /branches/release-35/php/lib/function.mtassetcount.php (revision 1956)
@@ -9,4 +9,4 @@
     $args['blog_id'] = $ctx->stash('blog_id');
     $count = $ctx->mt->db->asset_count($args);
-    return $count;
+    return $ctx->count_format($count, $args);
 }
Index: /branches/release-35/php/lib/MTViewer.php
===================================================================
--- /branches/release-35/php/lib/MTViewer.php (revision 1876)
+++ /branches/release-35/php/lib/MTViewer.php (revision 1956)
@@ -456,4 +456,23 @@
             return null;
     }
+
+    function count_format($count, $args) {
+        $phrase = '';
+        if ($count == 0) {
+            $phrase = array_key_exists('none', $args) ? $args['none'] :
+                (array_key_exists('plural', $args) ? $args['plural'] : '');
+        } elseif ($count == 1) {
+            $phrase = array_key_exists('singular', $args) ? $args['singular'] : '';
+        } elseif ($count > 1) {
+            $phrase = array_key_exists('plural', $args) ? $args['plural'] : '';
+        }
+        if ($phrase == '')
+            return $count;
+
+        // \# of entries: #  --> # of entries: 10
+        $phrase = preg_replace('/(?<!\\\\)#/', $count, $phrase);
+        $phrase = preg_replace('/\\\\#/', '#', $phrase);
+
+        return $phrase;
+    }
 }
-?>
Index: /branches/release-35/php/lib/function.mtblogcommentcount.php
===================================================================
--- /branches/release-35/php/lib/function.mtblogcommentcount.php (revision 1174)
+++ /branches/release-35/php/lib/function.mtblogcommentcount.php (revision 1956)
@@ -8,5 +8,5 @@
 function smarty_function_mtblogcommentcount($args, &$ctx) {
     $args['blog_id'] = $ctx->stash('blog_id');
-    return $ctx->mt->db->blog_comment_count($args);
+    $count = $ctx->mt->db->blog_comment_count($args);
+    return $ctx->count_format($count, $args);
 }
-?>
Index: /branches/release-35/php/lib/function.mtentriescount.php
===================================================================
--- /branches/release-35/php/lib/function.mtentriescount.php (revision 1174)
+++ /branches/release-35/php/lib/function.mtentriescount.php (revision 1956)
@@ -10,5 +10,5 @@
         return $ctx->tag('MTCategoryCount', $args);
     } elseif ($count = $ctx->stash('archive_count')) {
-        return $count;
+        # $count is set
     } else {
         $entries = $ctx->stash('entries');
@@ -21,8 +21,8 @@
         $lastn = $ctx->stash('_entries_lastn');
         if ($lastn && $lastn <= count($entries))
-            return $lastn;
+            $count = $lastn;
         else
-            return count($entries);
+            $count = count($entries);
     }
+    return $ctx->count_format($count, $args);
 }
-?>
Index: /branches/release-35/php/lib/function.mtarchivecount.php
===================================================================
--- /branches/release-35/php/lib/function.mtarchivecount.php (revision 1766)
+++ /branches/release-35/php/lib/function.mtarchivecount.php (revision 1956)
@@ -14,11 +14,12 @@
         $archiver = $_archivers[$at];
     }
+    $count = 0;
     if ((!isset($archiver) && $ctx->stash('inside_mt_categories')) ||
         ($ctx->stash('inside_mt_categories') && !$archiver->is_date_based())) {
         return $ctx->tag('MTCategoryCount', $args);
     } elseif ($count = $ctx->stash('archive_count')) {
-        return $count;
+        # $count is set
     } elseif ($entries = $ctx->stash('entries')) {
-        return count($entries);
+        $count = count($entries);
     } else {
         $eargs = array();
@@ -38,8 +39,7 @@
             $eargs['lastn'] = -1;
             $entries =& $ctx->mt->db->fetch_entries($eargs);
-            return count($entries);
+            $count = count($entries);
         }
     }
-    return 0;
+    $ctx->count_format($count, $args);
 }
-?>
Index: /branches/release-35/php/lib/function.mtentrycommentcount.php
===================================================================
--- /branches/release-35/php/lib/function.mtentrycommentcount.php (revision 1583)
+++ /branches/release-35/php/lib/function.mtentrycommentcount.php (revision 1956)
@@ -8,5 +8,5 @@
 function smarty_function_mtentrycommentcount($args, &$ctx) {
     $entry = $ctx->stash('entry');
-    return $entry['entry_comment_count'];
+    $count = $entry['entry_comment_count'];
+    return $ctx->count_format($count, $args);
 }
-?>
Index: /branches/release-35/php/lib/function.mtfoldercount.php
===================================================================
--- /branches/release-35/php/lib/function.mtfoldercount.php (revision 1174)
+++ /branches/release-35/php/lib/function.mtfoldercount.php (revision 1956)
@@ -10,3 +10,2 @@
     return smarty_function_mtcategorycount($args, $ctx);
 }
-?>
Index: /branches/release-35/php/lib/function.mtcategorycount.php
===================================================================
--- /branches/release-35/php/lib/function.mtcategorycount.php (revision 1174)
+++ /branches/release-35/php/lib/function.mtcategorycount.php (revision 1956)
@@ -8,5 +8,5 @@
 function smarty_function_mtcategorycount($args, &$ctx) {
     $category = $ctx->stash('category');
-    return $category['category_count'];
+    $count = $category['category_count'];
+    return $ctx->count_format($count, $args);
 }
-?>
Index: /branches/release-35/lib/MT/Template/ContextHandlers.pm
===================================================================
--- /branches/release-35/lib/MT/Template/ContextHandlers.pm (revision 1953)
+++ /branches/release-35/lib/MT/Template/ContextHandlers.pm (revision 1956)
@@ -2143,4 +2143,25 @@
 }
 
+sub _count_format {
+    my ($count, $args) = @_;
+    my $phrase;
+    $count ||= 0;
+    if ($count == 0) {
+        $phrase = exists $args->{none}
+            ? $args->{none}   : (exists $args->{plural}
+            ? $args->{plural} : '');
+    } elsif ($count == 1) {
+        $phrase = exists $args->{singular} ? $args->{singular} : '';
+    } elsif ($count > 1) {
+        $phrase = exists $args->{plural} ? $args->{plural} : '';
+    }
+    return $count if $phrase eq '';
+    return $phrase unless $phrase =~ m/#/;
+
+    $phrase =~ s/(?<!\\)#/$count/g;
+    $phrase =~ s/\\#/#/g;
+    return $phrase;
+}
+
 sub _hdlr_tag_count {
     my ($ctx, $args, $cond) = @_;
@@ -2148,10 +2169,14 @@
     my $tag = $ctx->stash('Tag');
     my $blog_id = $ctx->stash('blog_id');
-    return 0 unless $tag;
-    unless (defined $count) {
-        $count = MT::Entry->tagged_count($tag->id, { status => MT::Entry::RELEASE(),
-                                                     blog_id => $blog_id });
-    }
-    $count || 0;
+    if ($tag) {
+        unless (defined $count) {
+            $count = MT::Entry->tagged_count($tag->id, {
+                status => MT::Entry::RELEASE(),
+                blog_id => $blog_id
+            });
+        }
+    }
+    $count ||= 0;
+    return _count_format($count, $args);
 }
 
@@ -3557,5 +3582,6 @@
     $ctx->set_blog_load_context($args, \%terms, \%args)
         or return $ctx->error($ctx->errstr);
-    MT::Category->count(\%terms, \%args);
+    my $count = MT::Category->count(\%terms, \%args);
+    return _count_format($count, $args);
 }
                 
@@ -3568,5 +3594,6 @@
         or return $ctx->error($ctx->errstr);
     $terms{status} = MT::Entry::RELEASE();
-    $class->count(\%terms, \%args);
+    my $count = $class->count(\%terms, \%args);
+    return _count_format($count, $args);
 }
 
@@ -3578,5 +3605,6 @@
     $terms{visible} = 1;
     require MT::Comment;
-    MT::Comment->count(\%terms, \%args);
+    my $count = MT::Comment->count(\%terms, \%args);
+    return _count_format($count, $args);
 }
 
@@ -3589,6 +3617,7 @@
     require MT::Trackback;
     require MT::TBPing;
-    MT::Trackback->count(undef,
+    my $count = MT::Trackback->count(undef,
         { 'join' => MT::TBPing->join_on('tb_id', \%terms, \%args) });
+    return _count_format($count, $args);
 }
 
@@ -4260,5 +4289,8 @@
     my $e = $ctx->stash('entries');   
 
-    unless ($e) {
+    my $count;
+    if ($e) {
+        $count = scalar @$e;
+    } else {
         my $class_type = $args->{class_type} || 'entry';
         my $class = MT->model($class_type);
@@ -4296,9 +4328,7 @@
             $i++;
         }
-        return $i;
-    }
-
-    return 0 unless $e;
-    scalar @$e;
+        $count = $i;
+    }
+    return _count_format($count, $args);
 }  
 
@@ -4832,13 +4862,19 @@
 
 sub _hdlr_entry_comments {
-    my $e = $_[0]->stash('entry')
-        or return $_[0]->_no_entry_error($_[0]->stash('tag'));
-    $e->comment_count;
-}
+    my ($ctx, $args, $cond) = @_;
+    my $e = $ctx->stash('entry')
+        or return $ctx->_no_entry_error($ctx->stash('tag'));
+    my $count = $e->comment_count;
+    return _count_format($count, $args);
+}
+
 sub _hdlr_entry_ping_count {
-    my $e = $_[0]->stash('entry')
-        or return $_[0]->_no_entry_error($_[0]->stash('tag'));
-    $e->ping_count;
-}
+    my ($ctx, $args, $cond) = @_;
+    my $e = $ctx->stash('entry')
+        or return $ctx->_no_entry_error($ctx->stash('tag'));
+    my $count = $e->ping_count;
+    return _count_format($count, $args);
+}
+
 sub _hdlr_entry_previous {
     _hdlr_entry_nextprev('previous', @_);
@@ -5338,5 +5374,5 @@
     $name = '' unless defined $name;
     $name ||= $_[1]{default_name};
-    $name ||= '';
+    $name ||= MT->translate("Anonymous");
     my $show_email = $args->{show_email} ? 1 : 0;
     my $show_url = 1 unless exists $args->{show_url} && !$args->{show_url};
@@ -5487,5 +5523,5 @@
         or return $_[0]->_no_comment_error('MTCommentParentID');
     my $id = $c->parent_id || 0;
-    $args && $args->{pad} ? (sprintf "%06d", $id) : $id;
+    $args && $args->{pad} ? (sprintf "%06d", $id) : ($id ? $id : '');
 }
 
@@ -6161,5 +6197,5 @@
 
 sub _hdlr_archive_count {
-    my $ctx = $_[0];
+    my ($ctx, $args, $cond) = @_;
     my $at = $ctx->{current_archive_type} || $ctx->{archive_type};
     my $archiver = MT->publisher->archiver($at);
@@ -6167,10 +6203,11 @@
         return _hdlr_category_count($ctx);
     } elsif (my $count = $ctx->stash('archive_count')) {
-        return $count;
-    } else {
-        my $e = $_[0]->stash('entries');
-        my @entries = @$e if ref($e) eq 'ARRAY';
-        return scalar @entries;
-    }
+        return _count_format($count, $args);
+    }
+
+    my $e = $_[0]->stash('entries');
+    my @entries = @$e if ref($e) eq 'ARRAY';
+    my $count = scalar @entries;
+    return _count_format($count, $args);
 }
 
@@ -6433,5 +6470,5 @@
 
 sub _hdlr_category_count {
-    my($ctx) = @_;
+    my ($ctx, $args, $cond) = @_;
     my $cat = ($ctx->stash('category') || $_[0]->stash('archive_category'))
         or return $_[0]->error(MT->translate(
@@ -6449,9 +6486,9 @@
         $count = scalar $class->count(@args);
     }
-    $count;
+    return _count_format($count, $args);
 }
 
 sub _hdlr_category_comment_count {
-    my($ctx) = @_;
+    my ($ctx, $args, $cond) = @_;
     my $cat = ($ctx->stash('category') || $_[0]->stash('archive_category'))
         or return $_[0]->error(MT->translate(
@@ -6470,5 +6507,5 @@
     require MT::Comment;
     $count = scalar MT::Comment->count(@args);
-    $count;
+    return _count_format($count, $args);
 }
 
@@ -6541,5 +6578,5 @@
     require MT::TBPing;
     my $count = MT::TBPing->count( { tb_id => $tb->id, visible => 1 } );
-    $count || 0;
+    return _count_format($count || 0, $args);
 }
 
@@ -8153,5 +8190,6 @@
     $terms{blog_id} = $ctx->stash('blog_id') if $ctx->stash('blog_id');
     $terms{class} = $args->{type} || '*';
-    MT::Asset->count(\%terms, \%args);
+    my $count = MT::Asset->count(\%terms, \%args);
+    return _count_format($count, $args);
 }
  
@@ -8348,5 +8386,5 @@
     require MT::Page;
     $args->{class_type} = MT::Page->properties->{class_type};
-    &_hdlr_blog_entry_count(@_);
+    return _hdlr_blog_entry_count(@_);
 }
 
@@ -8417,40 +8455,40 @@
     require MT::Folder;
     $args->{class_type} = MT::Folder->properties->{class_type};
-    _hdlr_top_level_parent($ctx, $args, $cond);
+    return _hdlr_top_level_parent($ctx, $args, $cond);
 }
 
 sub _hdlr_folder_basename {
     return $_ unless &_check_folder(@_);
-    &_hdlr_category_basename(@_);
+    return _hdlr_category_basename(@_);
 }
 
 sub _hdlr_folder_description {
     return $_ unless &_check_folder(@_);
-    &_hdlr_category_desc(@_);
+    return _hdlr_category_desc(@_);
 }
 
 sub _hdlr_folder_id {
     return $_ unless &_check_folder(@_);
-    &_hdlr_category_id(@_);
+    return _hdlr_category_id(@_);
 }
 
 sub _hdlr_folder_label {
     return $_ unless &_check_folder(@_);
-    &_hdlr_category_label(@_);
+    return _hdlr_category_label(@_);
 }
 
 sub _hdlr_folder_count {
     return $_ unless &_check_folder(@_);
-    &_hdlr_category_count(@_);
+    return _hdlr_category_count(@_);
 }
 
 sub _hdlr_folder_path {
     return $_ unless &_check_folder(@_);
-    &_hdlr_sub_category_path(@_);
+    return _hdlr_sub_category_path(@_);
 }
 
 sub _hdlr_if_folder {
     return $_ unless &_check_folder(@_);
-    &_hdlr_if_category(@_);
+    return _hdlr_if_category(@_);
 }
 
@@ -8614,4 +8652,5 @@
 }
 
+# FIXME: should this routine return an empty string?
 sub _object_score_count {
     my ($stash_key, $ctx, $args, $cond) = @_;
@@ -8620,5 +8659,6 @@
     my $object = $ctx->stash($stash_key);
     return '' unless $object;
-    return $object->vote_for($key);
+    my $count = $object->vote_for($key);
+    return _count_format($count, $args);
 }
 
