]> git.deb.at Git - deb/packages.git/commitdiff
Use Number::Format to format numbers without the help of locales
authorFrank Lichtenheld <frank@lichtenheld.de>
Sat, 17 Nov 2007 19:56:56 +0000 (20:56 +0100)
committerFrank Lichtenheld <frank@lichtenheld.de>
Sat, 17 Nov 2007 20:46:07 +0000 (21:46 +0100)
Include Template::Plugin::Number::Format since that is not
packaged for Debian.

decimal_point and thousands_sep are configured via the
templates po files.

lib/Packages/DoDownload.pm
lib/Template/Plugin/Number/Format.pm [new file with mode: 0644]
templates/config.tmpl
templates/html/download.tmpl
templates/html/show.tmpl

index d999b9a042d6d06853c4ab9652e1bf4e68d9cf07..a871afd031bddcf4668ff86a2be15306b33b3b93 100644 (file)
@@ -89,10 +89,10 @@ sub do_download {
            $page_content->{pkg} = $pkg;
            my $pkgsize = floor(($data{size}/102.4)+0.5)/10;
            if ($pkgsize < 1024) {
            $page_content->{pkg} = $pkg;
            my $pkgsize = floor(($data{size}/102.4)+0.5)/10;
            if ($pkgsize < 1024) {
-               $page_content->{pkgsize} = sprintf( '%.1f', $pkgsize );
+               $page_content->{pkgsize} = $pkgsize;
                $page_content->{pkgsize_unit} = $cat->g( 'kByte' );
            } else {
                $page_content->{pkgsize_unit} = $cat->g( 'kByte' );
            } else {
-               $page_content->{pkgsize} = sprintf( '%.1f', floor(($data{size}/(102.4*102.4))+0.5)/100 );
+               $page_content->{pkgsize} = floor(($data{size}/(102.4*102.4))+0.5)/100;
                $page_content->{pkgsize_unit} = $cat->g( 'MByte' );
            }
            $page_content->{architecture} = $arch;
                $page_content->{pkgsize_unit} = $cat->g( 'MByte' );
            }
            $page_content->{architecture} = $arch;
diff --git a/lib/Template/Plugin/Number/Format.pm b/lib/Template/Plugin/Number/Format.pm
new file mode 100644 (file)
index 0000000..b089ea6
--- /dev/null
@@ -0,0 +1,258 @@
+package Template::Plugin::Number::Format;
+
+# ----------------------------------------------------------------------
+# $Id: Format.pm,v 1.1 2002/07/30 12:13:40 dlc Exp dlc $
+# ----------------------------------------------------------------------
+#  Template::Plugin::Number::Format - Plugin/filter interface to Number::Format
+#  Copyright (C) 2002 darren chamberlain <darren@cpan.org>
+#
+#  This program is free software; you can redistribute it and/or
+#  modify it under the terms of the GNU General Public License as
+#  published by the Free Software Foundation; version 2.
+#
+#  This program is distributed in the hope that it will be useful, but
+#  WITHOUT ANY WARRANTY; without even the implied warranty of
+#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+#  General Public License for more details.
+#
+#  You should have received a copy of the GNU General Public License
+#  along with this program; if not, write to the Free Software
+#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+#  02111-1307  USA
+# -------------------------------------------------------------------
+
+use strict;
+use vars qw($VERSION $DYNAMIC $AUTOLOAD);
+
+$VERSION = '1.02';
+$DYNAMIC = 1;
+
+use Number::Format;
+use base qw(Template::Plugin::Filter);
+
+# ----------------------------------------------------------------------
+# filter($text)
+#
+# The default filter is format_number, i.e., commify.
+# ----------------------------------------------------------------------
+sub filter {
+    my ($self, $text, $args) = @_;
+    $self->{ _NFO }->format_number($text, @$args);
+}
+
+
+# ----------------------------------------------------------------------
+# init($config)
+#
+# Initialize the instance.  Creates a Number::Format object, which is
+# used to create closures that implement the filters.
+# ----------------------------------------------------------------------
+sub init {
+    my ($self, $config) = @_;
+    my ($sub, $filter, $nfo);
+    $nfo = Number::Format->new(%$config);
+    
+    $self->{ _DYNAMIC } = 1;
+    $self->{ _NFO } = $nfo;
+
+    # ------------------------------------------------------------------
+    # This makes is dependant upon Number::Format not changing the 
+    # Exporter interface it advertises, which is unlikely.
+    #
+    # It is likely that each of these subroutines should accept all
+    # the configuration options of the constructor, and instantiate a
+    # new Number::Format instance.  This is easier, for now.
+    # ------------------------------------------------------------------
+    for my $sub (@{$Number::Format::EXPORT_TAGS{"subs"}}) {
+        my $filter = sub {
+            my ($context, @args) = @_;
+            return sub {
+                my $text = shift;
+                return $nfo->$sub($text, @args);
+            };
+        };
+        $self->{ _CONTEXT }->define_filter($sub, $filter, 1);
+    }
+
+    return $self;
+}
+
+# ----------------------------------------------------------------------
+# AUTOLOAD
+#
+# Catches method calls; so that the plugin can be used like you'd
+# expect a plugin to work:
+#
+# [% USE nf = Number.Format; nf.format_number(num) %]
+# ----------------------------------------------------------------------
+sub AUTOLOAD {
+    my $self = shift;
+   (my $autoload = $AUTOLOAD) =~ s/.*:://;
+
+    return if $autoload eq 'DESTROY';
+
+    $self->{ _NFO }->$autoload(@_);
+}
+
+1;
+
+__END__
+
+=head1 NAME
+
+Template::Plugin::Number::Format - Plugin/filter interface to Number::Format
+
+=head1 SYNOPSIS
+
+    [% USE Number.Format %]
+    [% num | format_number %]
+
+=head1 ABSTRACT
+
+Template::Plugin::Number::Format makes the number-munging grooviness
+of Number::Format available to your templates.  It is used like a
+plugin, but installs filters into the current context.
+
+=head1 DESCRIPTION
+
+All filters created by Template::Plugin::Number::Format can be
+configured by constructor options and options that can be passed to
+individual filters.  See L<Number::Format/"METHODS"> for all the details.
+
+=head2 Constructor Parameters
+
+The USE line accepts the following parameters, all optional, which
+define the default behavior for filters within the current Context:
+
+=over 4
+
+=item THOUSANDS_SEP
+
+character inserted between groups of 3 digits
+
+=item DECIMAL_POINT
+
+character separating integer and fractional parts
+
+=item MON_THOUSANDS_SEP
+
+like THOUSANDS_SEP, but used for format_price
+
+=item MON_DECIMAL_POINT
+
+like DECIMAL_POINT, but used for format_price
+
+=item INT_CURR_SYMBOL
+
+character(s) denoting currency (see format_price())
+
+=item DECIMAL_DIGITS
+
+number of digits to the right of dec point (def 2)
+
+=item DECIMAL_FILL
+
+boolean; whether to add zeroes to fill out decimal
+
+=item NEG_FORMAT
+
+format to display negative numbers (def -x)
+
+=item KILO_SUFFIX
+
+suffix to add when format_bytes formats kilobytes
+
+=item MEGA_SUFFIX
+
+suffix to add when format_bytes formats megabytes
+
+=item GIGA_SUFFIX
+
+suffix to add when format_bytes formats gigabytes
+
+=back
+
+=head1 Using Template::Plugin::Number::Format
+
+When you invoke:
+
+    [% USE Number.Format(option = value) %]
+
+the following filters are installed into the current Context:
+
+=over 4
+
+=item B<round($precision)>
+
+Rounds the number to the specified precision.  If "$precision" is
+omitted, the value of the "DECIMAL_DIGITS" parameter is used
+(default value 2).
+
+=item B<format_number($precision, $trailing_zeros)>
+
+Formats a number by adding "THOUSANDS_SEP" between each set of 3
+digits to the left of the decimal point, substituting "DECIMAL_POINT"
+for the decimal point, and rounding to the specified precision using
+"round()".  Note that "$precision" is a maximum precision specifier;
+trailing zeroes will only appear in the output if "$trailing_zeroes"
+is provided, or the parameter "DECIMAL_FILL" is set, with a value that
+is true (not zero, undef, or the empty string).  If "$precision" is
+omitted, the value of the "DECIMAL_DIGITS" parameter (default value
+of 2) is used.
+
+=item B<format_negative($picture)>
+
+Formats a negative number.  Picture should be a string that contains
+the letter "x" where the number should be inserted.  For example, for
+standard negative numbers you might use "-x", while for
+accounting purposes you might use "(x)".  If the specified number
+begins with a - character, that will be removed before formatting, but
+formatting will occur whether or not the number is negative.
+
+=item B<format_picture($picture)>
+
+Returns a string based on "$picture" with the "#" characters replaced
+by digits from "$number".  If the length of the integer part of
+$number is too large to fit, the "#" characters are replaced with
+asterisks ("*") instead. 
+
+=item B<format_price($precision)>
+
+Returns a string containing "$number" formatted similarly to
+"format_number()", except that the decimal portion may have trailing
+zeroes added to make it be exactly "$precision" characters long, and
+the currency string will be prefixed.
+
+If the "INT_CURR_SYMBOL" attribute of the object is the empty string,
+no currency will be added.
+
+If "$precision" is not provided, the default of 2 will be used. 
+
+=item B<format_bytes($precision)>
+
+Returns a string containing "$number" formatted similarly to
+"format_number()", except that if the number is over 1024, it will be
+divided by 1024 and the value of KILO_SUFFIX appended to the end; or
+if it is over 1048576 (1024*1024), it will be divided by 1048576 and
+MEGA_SUFFIX appended to the end.  Negative values will result in an
+error.
+
+If "$precision" is not provided, the default of 2 will be used.
+
+=item B<unformat_number>
+
+Converts a string as returned by "format_number()", "format_price()",
+or "format_picture()", and returns the corresponding value as a
+numeric scalar.  Returns "undef" if the number does not contain any
+digits.
+
+=back
+
+=head1 SEE ALSO
+
+L<Template|Template>, L<Number::Format|Number::Format>
+
+=head1 AUTHOR
+
+darren chamberlain E<lt>darren@cpan.orgE<gt>
+
index def9bae5351a6f23bab2ec8754a9a0789225f7c5..1fee16b8b4a0245fc268afb4c50235cb96b5b32f 100644 (file)
@@ -52,3 +52,7 @@
    sitewidemsg = { importance => "high",
                   txt => g('Please note that this is an experimental version of <a href="http://packages.debian.org/">packages.debian.org</a>. Errors and obsolete information should be expected') }
 -%]
    sitewidemsg = { importance => "high",
                   txt => g('Please note that this is an experimental version of <a href="http://packages.debian.org/">packages.debian.org</a>. Errors and obsolete information should be expected') }
 -%]
+[%# @translators: . = decimal_point , = thousands_sep, see Number::Format %]
+[%- USE num = Number.Format( decimal_point => g('.'),
+                            thousands_sep => g(','),
+                            decimal_fill  => 1 ) -%]
index f88d28fd997beb6d766e92fa0006bc3937236657..8cd30c5fbce5681a862d4bcb5de47de6aab0b269 100644 (file)
@@ -99,7 +99,7 @@ deb [% 'http://' _ security_mirror_url %] [% suite _ security_suite_suffix %] ma
 
 <h3>[% g('More information on <kbd>%s</kbd>:', filename.file) %]</h3>
 <table id="pdownloadmeta">
 
 <h3>[% g('More information on <kbd>%s</kbd>:', filename.file) %]</h3>
 <table id="pdownloadmeta">
-<tr><th>[% g('Exact Size') %]</th>     <td class="size">[% g('%s Byte (%s %s)', size, pkgsize, pkgsize_unit) %]</td>
+<tr><th>[% g('Exact Size') %]</th>     <td class="size">[% g('%s Byte (%s %s)', size, num.format_number(pkgsize,1), pkgsize_unit) %]</td>
 <tr><th>[% g('MD5 checksum') %]</th>   <td><tt>[% md5sum %]</tt></td>
 <tr><th>[% g('SHA1 checksum') %]</th>  <td><tt>[% sha1 || g('Not Available') %]</tt></td>
 <tr><th>[% g('SHA256 checksum') %]</th>        <td><tt>[% sha256  || g('Not Available') %]</tt></td>
 <tr><th>[% g('MD5 checksum') %]</th>   <td><tt>[% md5sum %]</tt></td>
 <tr><th>[% g('SHA1 checksum') %]</th>  <td><tt>[% sha1 || g('Not Available') %]</tt></td>
 <tr><th>[% g('SHA256 checksum') %]</th>        <td><tt>[% sha256  || g('Not Available') %]</tt></td>
index 44e17f8ff248b12962dc80cdda9c2f91a12ad60f..b6179f9ada3d0f543a052b059cd03d98ff930782 100644 (file)
        version_class = 'vold';
    END %]
 [% "<td class='$version_class'>$d.version</td>" IF versions.multiple %]
        version_class = 'vold';
    END %]
 [% "<td class='$version_class'>$d.version</td>" IF versions.multiple %]
-<td class="size">[% g('%.1f&nbsp;kB', d.pkgsize) %]</td><td class="size">[% g('%u&nbsp;kB', d.instsize) %]</td>
+<td class="size">[% g('%s&nbsp;kB', num.format_number(d.pkgsize,1)) %]</td><td class="size">[% g('%s&nbsp;kB', d.instsize) %]</td>
 <td>
 [% IF d.contents_avail %]
   [<a href="[% make_url(filelist_url) %]">[% g('list of files') %]</a>]
 <td>
 [% IF d.contents_avail %]
   [<a href="[% make_url(filelist_url) %]">[% g('list of files') %]</a>]
 
 <tr>
 <td><a href="[% server _ path %]">[% filename %]</a></td>
 
 <tr>
 <td><a href="[% server _ path %]">[% filename %]</a></td>
-<td>[% g('%.1f&nbsp;kB', size) %]</td>
+<td>[% g('%s&nbsp;kB', num.format_number(size,1)) %]</td>
 <td class="md5sum">[% md5sum %]</td>
 </tr>
 
 <td class="md5sum">[% md5sum %]</td>
 </tr>