aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--perllib/FixMyStreet/App/Controller/Report/New.pm6
-rw-r--r--t/i18n.t70
2 files changed, 74 insertions, 2 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Report/New.pm b/perllib/FixMyStreet/App/Controller/Report/New.pm
index 078b0b994..46a90384c 100644
--- a/perllib/FixMyStreet/App/Controller/Report/New.pm
+++ b/perllib/FixMyStreet/App/Controller/Report/New.pm
@@ -7,8 +7,8 @@ BEGIN { extends 'Catalyst::Controller'; }
use FixMyStreet::Geocode;
use Encode;
use Image::Magick;
-use Sort::Key qw(keysort);
use List::MoreUtils qw(uniq);
+use POSIX 'strcoll';
use HTML::Entities;
use mySociety::MaPit;
use Path::Class;
@@ -479,7 +479,9 @@ sub setup_categories_and_councils : Private {
} else {
- @contacts = keysort { $_->category } @contacts; # TODO Old code used strcoll, check if this no longer needed
+ # keysort does not appear to obey locale so use strcoll (see i18n.t)
+ @contacts = sort { strcoll( $a->category, $b->category ) } @contacts;
+
my %seen;
foreach my $contact (@contacts) {
diff --git a/t/i18n.t b/t/i18n.t
index 6a5d94fa2..a5b68782b 100644
--- a/t/i18n.t
+++ b/t/i18n.t
@@ -5,6 +5,12 @@ use Test::More;
use FixMyStreet;
use mySociety::Locale;
+use Encode;
+use Data::Dumper;
+use Sort::Key qw(keysort);
+use POSIX 'strcoll';
+local $Data::Dumper::Sortkeys = 1;
+use utf8;
# check that the mo files have been generated
die "You need to run 'commonlib/bin/gettext-makemo --quiet FixMyStreet' "
@@ -36,4 +42,68 @@ mySociety::Locale::gettext_domain( 'FixMyStreet-EmptyHomes', 1,
mySociety::Locale::change('cy');
is _($english), $welsh, "english to welsh (deep directory)";
+# test that sorting works as expected in the right circumstances...
+my @random_sorted = qw( Å Z Ø A );
+my @EN_sorted = qw( A Å Ø Z );
+my @NO_sorted = qw( A Z Ø Å );
+my @default_sorted = qw( A Z Å Ø );
+
+sub utf8_diag {
+ diag encode_utf8( Dumper(@_) );
+}
+
+{
+
+ mySociety::Locale::negotiate_language( #
+ 'en-gb,English,en_GB|cy,Cymraeg,cy_GB', 'en_GB'
+ );
+ mySociety::Locale::change();
+
+ no locale;
+
+ is_deeply( [ sort @random_sorted ],
+ \@default_sorted, "sort correctly with no locale" );
+
+ is_deeply( [ keysort { $_ } @random_sorted ],
+ \@default_sorted, "keysort correctly with no locale" );
+
+ # Note - this obeys the locale
+ is_deeply( [ sort { strcoll( $a, $b ) } @random_sorted ],
+ \@EN_sorted, "sort strcoll correctly with no locale (to 'en_GB')" );
+}
+
+{
+ mySociety::Locale::negotiate_language( #
+ 'en-gb,English,en_GB|cy,Cymraeg,cy_GB', 'en_GB'
+ );
+ mySociety::Locale::change();
+ use locale;
+
+ is_deeply( [ sort @random_sorted ],
+ \@EN_sorted, "sort correctly with use locale 'en_GB'" );
+
+ # is_deeply( [ keysort { $_ } @random_sorted ],
+ # \@EN_sorted, "keysort correctly with use locale 'en_GB'" );
+
+ is_deeply( [ sort { strcoll( $a, $b ) } @random_sorted ],
+ \@EN_sorted, "sort strcoll correctly with use locale 'en_GB'" );
+}
+
+{
+ mySociety::Locale::negotiate_language( #
+ 'nb-no,Norwegian,nb_NO', 'nb_NO'
+ );
+ mySociety::Locale::change();
+ use locale;
+
+ is_deeply( [ sort @random_sorted ],
+ \@NO_sorted, "sort correctly with use locale 'nb_NO'" );
+
+ # is_deeply( [ keysort { $_ } @random_sorted ],
+ # \@NO_sorted, "keysort correctly with use locale 'nb_NO'" );
+
+ is_deeply( [ sort { strcoll( $a, $b ) } @random_sorted ],
+ \@NO_sorted, "sort strcoll correctly with use locale 'nb_NO'" );
+}
+
done_testing();