[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Sorting is implemented in cdebconf CVS (was Re: Countrychooser commited in CVS)



On Fri, Jan 16, 2004 at 09:07:56PM +0100, Denis Barbier wrote:
> On Fri, Jan 16, 2004 at 07:15:28AM +0100, Christian Perrier wrote:
> [...]
> > I first need to add some sorting in English.....and after this, we
> > will be thrown in the known problem of translations sorting (which is
> > not countrychooser specific, by the way)
> 
> This requires changes in cdebconf, so you should not focus on it now.

Sorting cannot be performed at run-time because locales package is not
installed.  Sorting items when building packages is painful because
the needed UTF-8 locales have to be installed.  So the only option is
to statically build sorted lists.  Thus I implemented a new field in
cdebconf to tell in which order items have to be displayed.  Consider
for instance:
  Choices: a, b, c
  Choices-ll.UTF-8: x, y, z
  Indices-ll.UTF-8: 3, 1, 2
The first 2 lines have their usual meaning, ie. 'a' is translated into
'x', 'b' into 'y' and 'c' into 'z'.  The Indices field tells that items
must be displayed as "z, x, y" for this language.
This has been tested on countrychooser with the help of the attached
script;
  $ sort_countries --lang=fr --locale=fr_FR.UTF-8 \
          < debian/templates > debian/templates.sorted
Now if templates is replaced by templates.sorted, countries are sorted.

Remaining issues:
  * iso-codes French translation contains commas, which confuses
    cdebconf.  Other languages have surely the same problem.
  * This script should be split into 2 parts:
      a. print the sorted list (ie $output) into a file sorted.$lang
      b. read sorted.$lang files and add Indices fields.
    This way sorted.$lang files could be put under CVS wings and
    translators might check that countries are sorted as expected.
    Maintainer can run (b) without having locales installed.

Denis
#! /usr/bin/perl -w

use Getopt::Long;
use POSIX;
use locale;

binmode STDIN, ':utf8';
binmode STDOUT, ':utf8';

my $lang = '';
my $locale = '';
GetOptions ("lang=s" => \$lang, "locale=s" => \$locale);

die "Usage: sort_countries --lang=langcode --locale=locale"
        unless length($lang) > 0 && length($locale) > 0;
setlocale(LC_ALL, $locale);
my $cnt = 0;
while (<>) {
        print;
        next unless s/Choices-$lang.UTF-8: //;
        chomp;
        my @list = split(/, /);
        my @block = ();
        my $output = '';
        for my $country (@list) {
                chomp $country;
                if ($country =~ m/^\x{00a0}/) {
                        $cnt++;
                        push @block, $country." ".$cnt;
                } else {
                        $output .= join("\n", sort @block)."\n" if (@block);
                        @block = ();
                        $cnt++;
                        $output .= $country." ".$cnt."\n";
                }
        }
        $output .= join("\n", sort @block)."\n" if (@block);
        $output =~ s/.*? (\d+)$/$1/mg;
        $output =~ s/\n/, /g;
        $output =~ s/, $//;
        print "Indices-$lang.UTF-8: $output\n";
}
1;

Reply to: